龙空技术网

javaweb ssh struts 2.5相比于之前版本配置区别及问题研究

每天说说科技 100

前言:

现在大家对“apache struts2和apache tomcat”都比较看重,看官们都需要分析一些“apache struts2和apache tomcat”的相关知识。那么小编也在网络上汇集了一些对于“apache struts2和apache tomcat””的相关资讯,希望看官们能喜欢,兄弟们一起来了解一下吧!

struts自从升级到二点五以上可以说是非常难用,首先他删除了ng包将配置文件变成了这样

解决了can not find method for result …问题

解决了action method index问题

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

其实这样就这样把,也不是什么重要的内容,但是配置完事之后各种问题就非常尴尬,各种问题,然后还找不到定位点,忙活了一个多小时才给配置好

问题一: action 必须指定class

<action name="hello" class="com.csy.struct2.HelloStruct2

否则404

HTTP Status 404 - /Struct2msb/test/hellotype Status reportmessage /Struct2msb/test/hellodescription The requested resource is not available.Apache Tomcat/7.0.82
问题二:action HelloStruct2里面必须有index()方法,否则报错

问题三:执行action并且指定了method方法,但是结果却只执行index()方法

最后原因是多拷了一个lib导致的。最开始拷贝地demo里面的全部jar包

struts2-rest-plugin-2.5.14.1.jar

z

报错 Wrong method was defined as an action method: index (Action类里面没有定义index这个方法)

错误原因 :

按以往的理解 为了实现项目的零配置,采用struts2的注解方式进行配置,会直接调getHelloWorld方法。

那么为何会报错呢?

看了Struts2的调用全过程,发现其实调用mapper的时候 是直接指向org.apache.struts2.dispatcher.mapper.Restful2ActionMapper 而不是DefaultActionMapper,

这样就导致了调用到rest里面的

if (mapping.getMethod() == null) {

if (lastSlashPos == actionName.length() - 1) {

if (isGet(request)) {

mapping.setMethod("index");

} else if (isPost(request)) {

mapping.setMethod("create");

}

}

所以直接报NoSuchMethodException。

可配置文件中没有配置支持Restful2ActionMapper。

那是否struts2.3.24中jar包有插件直接支持呢?

果然发现了:struts2-rest-plugin-2.3.24.jar 这个包中的:struts-plugin.xml

<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="rest" class="org.apache.struts2.rest.RestActionProxyFactory" />

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="rest" class="org.apache.struts2.rest.RestActionMapper" />

<constant name="struts.mapper.class" value="rest" />

这个导致了Restful2ActionMapper的调用。

解决方法:删除struts2-rest-plugin-xxx.jar

问题四:struts-2.5.14.1需要添加哪些jar包,为什么配置正确后找不到action

struts-2.5.14.1-min-lib.zip (4MB) struts-2.5.14.1-min-lib.zip (4MB)

拷贝里面的lib即可

最后写到这里我们不得不提一下ModenDriven的问题,根本没人会用这个,首先我不解释原理,web这种东西,特别是框架,解释原理没多大意思,要是真想了解底层,不如去研究操作系统,初次之外我们会用就行,况且我也不是专门做web端的,仅仅是顺带着做一下这个,使用ModelDirven必须先继承

implements ModelDriven<MAMage>

其次在代码中必须自己先实例化一个对象

 private __Mamage__ thisObj = new __Mamage__();

这个对象必须是一个Javabean对象,只要参数名和html表单参数名相同就可以了,之后重写

getModel()方法。

@Overridepublic __Mamage__ getModel() { if (thisObj!=null) { return thisObj; } else { return new __Mamage__(); } }

其实我这里面判断为空意义不大,因为要是真是空那还真就出问题了。

最后在struts.xml配置

<package name="datachange" extends="struts-default"> <interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="checkbox"> <param name="uncheckedValue">0</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> </interceptor-stack> <interceptor name="ModelDrivenInterceptor" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"></interceptor> </interceptors> <action name="login" class="web_base.action.MamageLogin"> <interceptor-ref name="modelDriven"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="success">/index.jsp</result> </action> </package>

在自己定义的method方法里面可以使用自己的数据对象。

标签: #apache struts2和apache tomcat