1。jar包:struts2-codebehind-plugin-2.2.1.1.jar
2。struts.xml:<!-- codebehind中查找action的返回结果资源时的默认文件夹 -->
<constant name="struts.codebehind.pathPrefix" value="/pages/" />
3。web.xml:
1 <!-- struts2过滤器 --> 2 <filter> 3 <filter-name>struts2</filter-name> 4 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 5 <init-param> 6 <param-name>actionPackages</param-name> 7 <param-value> 8 com.ext.emp.struts2,com.demo.stu.struts2 9 </param-value> 10 </init-param> 11 </filter> 12 <!-- action过滤 --> 13 <filter-mapping> 14 <filter-name>struts2</filter-name> 15 <url-pattern>*.action</url-pattern> 16 </filter-mapping> 17 <!-- 静态资源过滤 --> 18 <filter-mapping> 19 <filter-name>struts2</filter-name> 20 <url-pattern>/struts/*</url-pattern> 21 </filter-mapping>
4。
1)Demo!doUpdate.action
2)Demo!update.action?empNo=<s:property value="empNo"/>
3)Demo!delete.action?empNo=<s:property value="empNo"/>
4)localhost:8080/empDemo/emp/Demo!create.action
5。
这里action的命名最好加上Action后缀,否则可能与pojo中的Demo同名(不会影响去找"Demo"这个Action:DemoAction)
package com.ext.emp.struts2.emp; public class DemoAction { // search show public String execute(){ // get list if (pagiParam == null) { pagiParam = new PagiParam(); } try { emps = empService.findToPage(empName, sortCol, PureUtils.isAsc(sortOrder), pagiParam, false); } catch (Exception e) { e.printStackTrace(); } return "success"; } public String update(){ emp = empService.findById(empNo); return "update"; } public String doUpdate(){ empService.update(emp); return execute(); } public String create(){ //go to create page return "create"; } public String doCreate(){ empService.save(emp); return execute(); } public String delete(){ Emp emp = new Emp(); emp.setEmpNo(empNo); empService.delete(emp); return execute(); } }
注意:
web.xml中actionPackages下com.bonc.ext.emp.struts2不能写成com.bonc.ext.emp.struts2.emp;
struts.xml限定了jsp的目录是在WebRoot/pages/下;
web.xml限定了action目录是在struts2/下;
对应一个请求localhost:8080/empDemo/emp/Demo!create.action,
会解析并判断是否有emp/DemoAction这个action,然后跳到create方法,这个方法返回“create”字符串,
然后解析并判断(Demo+‘-’+create+'jsp',及Demo-create.jsp)是否有emp/Demo-create.jsp在/pages/下,有的话即把这个页面返回给浏览器