struts2不是struts1的下一代產品,是在struts1和WebWork技術的基礎上進行合並后的全新框架,雖然兩個名字相似,但是設計思想卻有很大的不同。
使用本地的l ib 或者download都可以,這里我使用本地的包,后期比較方便,需要什么包可以直接在本地找到,並拷貝,不需要再去網絡上下載
工程命名以及存放位置
新建好工程之后,自行在 WEB-INF 下面新建 classes 和 lib 兩個文件夾,改變對應的目錄,這里說一下自己踩的坑,
自己使用的版本比較高,2.5.20;
在配置 web.xml 的時候,注意 idea 自動配置的是 2.5 一下的版本,里面的文件出現了變化,所以 filter-class 需要改變去掉 .ng 即可
開始的時候無論怎么操作都出現404,后來發現少了一下 lib 包,
后來找了一下,發現 2.5.20 版本把這個文件整合到 struts2-core-2.5.20.jar 里面了,那么在調用的時候在 struts.xml 中加上了
1 <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 2 <constant name="struts.devMode" value="true"/>
然后重新運行即可
工程目錄:
web.xml 配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <filter> 7 <filter-name>struts2</filter-name> 8 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 9 </filter> 10 <filter-mapping> 11 <filter-name>struts2</filter-name> 12 <url-pattern>/*</url-pattern> 13 </filter-mapping> 14 </web-app>
struts.xml 配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 6 <struts> 7 <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 8 <constant name="struts.devMode" value="true"/> 9 <package name="hello" namespace="/" extends="struts-default"> 10 <action name="helloWorld" class="com.hello.action.HelloWorldAction"> 11 <result name="success">/success.jsp</result> 12 </action> 13 </package> 14 </struts>
最重要的就是這兩個配置,還有其他步驟的設置