码迷,mamicode.com
首页 > 其他好文 > 详细

关于struts

时间:2015-08-17 23:09:57      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

一个最简单的struts程序竟然搞了一整天。感觉书上写的不对啊。书上说的导入的那几个包不全,会报错。

技术分享

信息: Deploying web application directory D:\apache-tomcat-7.0.52\webapps\struts2
八月 17, 2015 9:37:45 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
严重: Dispatcher initialization failed
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
 at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:301)
 at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:438)
 at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:207)
 at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:51)
 at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:93)
 at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:487)
 at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:484)
 at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:584)
 at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:484)
 at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createBootstrapContainer(DefaultConfiguration.java:351)
 at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:235)
 at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
 at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:967)
 at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:435)
 at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:479)
 at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
 at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57)
 at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
 at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
 at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
 at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4809)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5485)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
 at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1229)
 at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
 at java.util.concurrent.FutureTask.run(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:299)
 ... 32 more
Caused by: java.lang.ExceptionInInitializerError
 at com.opensymphony.xwork2.ognl.OgnlValueStackFactory.setContainer(OgnlValueStackFactory.java:84)
 ... 37 more
Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!
 at ognl.OgnlRuntime.<clinit>(OgnlRuntime.java:168)
 ... 38 more
Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)
 at ognl.OgnlRuntime.<clinit>(OgnlRuntime.java:165)
 ... 38 more
八月 17, 2015 9:37:45 下午 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter struts2
java.lang.reflect.InvocationTargetException - Class: com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector
File: ContainerImpl.java
Method: inject
Line: 301 - com/opensymphony/xwork2/inject/ContainerImpl.java:301:-1
 at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:493)
 at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
 at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57)
 at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
 at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
 at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
 at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4809)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5485)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
 at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1229)
 at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
 at java.util.concurrent.FutureTask.run(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
 at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:301)
 at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:438)
 at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:207)
 at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:51)
 at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:93)
 at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:487)
 at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:484)
 at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:584)
 at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:484)
 at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createBootstrapContainer(DefaultConfiguration.java:351)
 at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:235)
 at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
 at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:967)
 at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:435)
 at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:479)
 ... 18 more
Caused by: java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:299)
 ... 32 more
Caused by: java.lang.ExceptionInInitializerError
 at com.opensymphony.xwork2.ognl.OgnlValueStackFactory.setContainer(OgnlValueStackFactory.java:84)
 ... 37 more
Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!
 at ognl.OgnlRuntime.<clinit>(OgnlRuntime.java:168)
 ... 38 more
Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)
 at ognl.OgnlRuntime.<clinit>(OgnlRuntime.java:165)
 ... 38 more

解决办法:

1删除原项目lib下的所有jar包。

2struts目录下的struts\apps\struts2-blank-2.war。放入tomcat的webapps下。

3启动tomcat这个blank会自动解压。

4将其lib目录下的所有jar包放入项目lib下。

问题解决了。

真他妈的坑爹!

以后这种框架类的项目遇到问题首先就要去考虑包没导对。

 

继续谈struts。

第一个hello struts程序。

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name = "day01" namespace = "/demo" extends = "struts-default">
      <action name = "hello" class = "action.HelloAction" method = "sayHello" >
          <result name = "success">
               /hello.jsp
          </result>
      </action>
</package>
</struts>

 

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
    <!-- struts过滤器 -->
  <filter>
         <!-- 过滤器名称 -->
         <filter-name>struts2</filter-name>
         <!-- 过滤器类 -->
         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         </filter>
         <!-- Struts2 过滤器映射 -->
    <filter-mapping>
                  <!-- 过滤器名称 -->
         <filter-name>struts2</filter-name>
         <!-- 过滤器映射 -->
         <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>


action:

package action;

public class HelloAction {
    public String sayHello(){
        System.out.println("Hello,Action");
        return "success";
    }
}

 

hello.jsp:

<%@page pageEncoding = "utf-8"%>
<html>
    <head>
           <body>
                  <h1>Hello,Struts2.</h1>
          </body>
   </head>
</html>

 

基本属性注入与域模型注入

这两种方式能将表单提交的数据传递给action来处理。

index.jsp

     <form action = "/struts2/demo/hello.action" method = "post">
           <!-- 演示基本属性注入 -->
           姓名:<input type = "text" name = "realName"/><br/><br/>
           <!-- 演示域模型注入 -->
           用户名:<input type ="text" name = "user.userName"/><br/><br/>
              密码:<input type = "password" name = "user.passWord"/> <br/><br/>
           <input type = "submit" value = "提交"/>
      </form>

 User

package entity;

public class User {
    private String userName;
    private String passWord;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassWord() {
        return passWord;
    }
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
    
}

 

HelloAction

package action;

import entity.User;

public class HelloAction {
    public HelloAction(){
        System.out.println("实例化Action。");
    }
    //定义基本类型属性,接收表单参数:姓名
    private String realName;
    
    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }
    //定义实体对象属性,接收表单参数:用户名密码
    private User user;
    
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String sayHello(){
        System.out.println("Hello,Action");
        
        //输出基本类型数据
        System.out.println("姓名"+realName);
        
        //输出域模式方式注入的参数
        System.out.println("用户名"+user.getUserName());
        System.out.println("密码"+user.getPassWord());
        return "success";
    }
}

 

 如何将提交来的数据显示到result的页面里呢?

使用EL表达式可以实现。

<%@page pageEncoding = "utf-8"%>
<html>
    <head>
           <body>
                  <h1>Hello,Struts2.</h1>
                  <h1>姓名:${realName }</h1>
                  <h1>用户名:${user.userName }</h1>
                  <h1>密码:${user.passWord }</h1>
          </body>
   </head>
</html>

 

关于struts

标签:

原文地址:http://www.cnblogs.com/rixiang/p/4737906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!