Struts配置及404问题

来源:互联网 发布:淘宝互刷软件 编辑:程序博客网 时间:2024/04/30 21:23

胡扯 
在struts学习过程中,很多教程用的都是strut-2.3.31,而现在最新的是struts-2.5.10,升级之后的strut2技能总是会有点变化的。还以为只要对着教程敲就行了,没想到不停地报错,简直无法让人享受敲代码的乐趣呀!


struts-2.3.31

1. 引入jar

asm-3.3.jarasm-commons-3.3.jarasm-tree-3.3.jarcommons-fileupload-1.3.1.jarcommons-io-2.2.jarcommons-lang3-3.1.jarcommons-logging-1.1.3.jarfreemarker-2.3.19.jarjarlist.txtjavassist-3.11.0.GA.jarognl-3.0.6.jarstruts2-core-2.3.16.3.jarxwork-core-2.3.16.3.jar
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2. web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">  <filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping></web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3. 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>    <constant name="struts.devMode" value="true" />    <!-- 所有的Action定义都应该放在package下 -->       <package name="crazyit" namespace="/" extends="struts-default">             <action name="*">            <result>/WEB-INF/content/{1}.jsp</result>        </action>    </package></struts>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

struts-2.5.10

1. 引入jar

asm-5.1.jarasm-commons-5.1.jarasm-tree-5.1.jarcommons-fileupload-1.3.2.jarcommons-io-2.4.jarcommons-lang3-3.4.jarcommons-logging-1.1.3.jarfreemarker-2.3.23.jarjarlist.txtjavassist-3.20.0-GA.jarlog4j-api-2.7.jarognl-3.1.12.jarstruts2-core-2.5.10.jar
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

相对于struts-2.3.31,2.5.10版本需要导入的基本jar要少了xwork-core-2.3.16.3.jar,多了log4j-api-2.7.jar。由于xwork-core已经整合到struts-core中了,所以这里我们在下载struts-2.5.10/lib找不到xwork-core的字眼,但我们可以用解压软件打开struts-core查看内部的文件,可以发现到之前版本所没有的xwork。如果没有log4j-api-2.7.jar`,在web.xml和struts.xml配置正确的前提下,会报错如下错误:

log4j-api-2.7.jar缺少报错

2. web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app id="starter" version="2.4"         xmlns="http://java.sun.com/xml/ns/j2ee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <filter>        <filter-name>struts2</filter-name>        <!--         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>         -->         <!-- 以下路径中没有ng目录 -->        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

细心的读者可以发现一个很大的不同是,2.5.10版本配置<filter-class>指定类文件路径中少了一个ng。是的没有错,我们只要打开struts2-core-2.5.10.jar即可发现里面不存在子目录ng。也只有通过上面的路径才能找到类StrutsPrepareAndExecuteFilter。如果没有配置正确的路径就会报以下错误:

ClassNotFoundException

3. struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    "http://struts.apache.org/dtds/struts-2.5.dtd"><struts>    <constant name="struts.devMode" value="true" />    <!-- 所有的Action定义都应该放在package下 -->       <package name="crazyit" namespace="/" extends="struts-default">             <action name="*">            <result>/WEB-INF/content/{1}.jsp</result>        </action>    </package></struts>关于引入jar的问题,尽量引入上面所述的jar,而不是把所有jar都引入,否则就用可能一直出现404问题
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
原创粉丝点击