Struts2.5版本以上的struts.xml配置

由于Struts2的版本在不断的更新,对文件的配置要求也有了一些改变。

对于Struts2.5以上的版本如果需要url+!+方法访问Action某个方法的话需要在struts.xml加入如下语句

[html]  view plain  copy
  1. <constant name="struts.enable.DynamicMethodInvocation" value="true"/>  
  2. <constant name="struts.devMode" value="true"></constant>  

以上两句是DIM(动态访问犯法的配置)

有时候可以还会出现 Method 方法 for action Action is not allowed

这时候可能需要在struts.xml中package中加入继续加入如下语句

[java]  view plain  copy
  1. <global-allowed-methods>regex:.*</global-allowed-methods>  


还有别忘了把项目的输出设置到WEB-INF下的classes文件里,如图

图片

最后我附上一个配置的例子

在这之前你需要下载如下一下struts包并加载到libraries里,这些类可以去Apache官网下载

图片

有有时候有些包需要放到tomcat的lib里面才可以,不敢上述的包不需要

1、首先我们配置一下web.xml

[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
  5.     version="3.1">  
  6.     <display-name>Struts</display-name>  
  7.     <welcome-file-list>  
  8.         <welcome-file>index.html</welcome-file>  
  9.         <welcome-file>index.htm</welcome-file>  
  10.         <welcome-file>index.jsp</welcome-file>  
  11.         <welcome-file>default.html</welcome-file>  
  12.         <welcome-file>default.htm</welcome-file>  
  13.         <welcome-file>default.jsp</welcome-file>  
  14.     </welcome-file-list>  
  15.     <filter>  
  16.         <filter-name>Struts2</filter-name>  
  17.         <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  18.     </filter>  
  19.     <filter-mapping>  
  20.         <filter-name>Struts2</filter-name>  
  21.         <url-pattern>/*</url-pattern>  
  22.     </filter-mapping>  
  23. </web-app>  

其中org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter过滤器是struts2.5后由org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter改过而来的

2、配置struts.xml

[java]  view plain  copy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE struts PUBLIC  
  4.     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"  
  5.     "http://struts.apache.org/dtds/struts-2.5.dtd">  
  6.   
  7. <struts>  
  8. <constant name="struts.enable.DynamicMethodInvocation" value="true"/>  
  9. <constant name="struts.devMode" value="true"></constant>  
  10.     <package name="MyPackage" namespace="/" extends="struts-default">  
  11.         <global-allowed-methods>regex:.*</global-allowed-methods>  
  12.         <action name="first" class="UserAction">  
  13.           
  14.             <result name="success">first.jsp</result>  
  15.             <result name="add">add.jsp</result>  
  16.             <result name="delete">delete.jsp</result>  
  17.         </action>  
  18.           
  19.     </package>  
  20. </struts></span>  

这个如果嫌难打的话可以去源码包拷贝,class指定是action的类名

3、UserAction类

[java]  view plain  copy
  1. <span style="font-size:12px;">import com.opensymphony.xwork2.ActionSupport;  
  2.   
  3. public class UserAction extends ActionSupport {  
  4.   
  5.     /** 
  6.      *  
  7.      */  
  8.     private static final long serialVersionUID = 1L;  
  9.   
  10.     private String username;  
  11.       
  12.     private String info;  
  13.     public String getUsername() {  
  14.         return username;  
  15.     }  
  16.   
  17.     public void setUsername(String username) {  
  18.         this.username = username;  
  19.     }  
  20.       
  21.     public String add()throws Exception{  
  22.         setInfo("添加用户");  
  23.         return "add";  
  24.     }  
  25.       
  26.     public String delete()throws Exception{  
  27.         setInfo("删除用户");  
  28.         return "delete";  
  29.     }  
  30.   
  31.   
  32.     public String getInfo() {  
  33.         return info;  
  34.     }  
  35.   
  36.     public void setInfo(String info) {  
  37.         this.info = info;  
  38.     }  
  39.       
  40. }</span>  

4、index.jsp

[java]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. <link rel="stylesheet" type= "text/css" href="css/indescss.css">  
  9. </head>  
  10.   
  11. <body>  
  12.   
  13. <%String path = request.getContextPath(); %>  
  14. <a href="first!add" >go the add page</a>  
  15. <br>  
  16. <a href="first!delete" >go the delete page</a>  
  17. <br>  
  18. <a href="first.action" >go the first page</a>  
  19. <br>  
  20. <a href="add" >go the add2 page</a>  
  21. </body>  
  22. </html>  


5、delete.jsp

[html]  view plain  copy
  1. <span style="font-size:12px;"><%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11. <p><s:property value="info"/></p>  
  12. </body>  
  13. </html></span>  


6、add.jsp

[java]  view plain  copy
  1. <span style="font-size:12px;"><%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@taglib prefix="s" uri="/struts-tags" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11. <p><s:property value="info"/></p>  
  12. </body>  
  13. </html></span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值