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

来源:互联网 发布:linux网速测试工具 编辑:程序博客网 时间:2024/06/05 01:09

*Struts2.5版本以上的struts.xml和jar包配置 *

由于Struts2的版本在不断的更新,对文件的配置要求也有了一些改变。
对于Struts2.5以上的版本如果需要url+!+方法访问Action某个方法的话需要在struts.xml加入如下语句

    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>      <constant name="struts.devMode" value="true"></constant>  

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

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

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

<global-allowed-methods>regex:.*</global-allowed-methods>  

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

原因
JBoss默认从WEB-INF/目录下加载资源,Eclipse在发布程序的时候,并没有把User Libraries的相关资源拷贝到WEB-INF/目录下(Eclipse会把src目录下的所有非*.java文件复制到WEB-INF/classes目录下),所以Tomcat说找不到所需要的类

所需jar包
jar包

注意jar包一定要放在WebContent/WEB-INF/lib路径下 最后注意要build path

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

    <?xml version="1.0" encoding="UTF-8"?>      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xmlns="http://xmlns.jcp.org/xml/ns/javaee"          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"          version="3.1">          <display-name>Struts</display-name>          <welcome-file-list>              <welcome-file>index.html</welcome-file>              <welcome-file>index.htm</welcome-file>              <welcome-file>index.jsp</welcome-file>              <welcome-file>default.html</welcome-file>              <welcome-file>default.htm</welcome-file>              <welcome-file>default.jsp</welcome-file>          </welcome-file-list>          <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>      </web-app>  

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

2、配置struts.xml

    <span style="font-size:12px;"><?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.enable.DynamicMethodInvocation" value="true"/>      <constant name="struts.devMode" value="true"></constant>          <package name="MyPackage" namespace="/" extends="struts-default">              <global-allowed-methods>regex:.*</global-allowed-methods>              <action name="first" class="UserAction">                  <result name="success">first.jsp</result>                  <result name="add">add.jsp</result>                  <result name="delete">delete.jsp</result>              </action>          </package>      </struts></span>  

注意class指定是action的类名

3、UserAction类

import com.opensymphony.xwork2.ActionSupportpublic class UserAction extends ActionSupport {      /**      *       */      private static final long serialVersionUID = 1L;      private String username;      private String info;      public String getUsername() {          return username;      }      public void setUsername(String username) {          this.username = username;      }      public String add()throws Exception{          setInfo("添加用户");          return "add";      }      public String delete()throws Exception{          setInfo("删除用户");          return "delete";      }      public String getInfo() {          return info;      }      public void setInfo(String info) {          this.info = info;      }  }

4、index.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"          pageEncoding="UTF-8"%>      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">      <html>      <head>      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">      <title>Insert title here</title>      <link rel="stylesheet" type= "text/css" href="css/indescss.css">      </head>      <body>      <%String path = request.getContextPath(); %>      <a href="first!add" >go the add page</a>      <br>      <a href="first!delete" >go the delete page</a>      <br>      <a href="first.action" >go the first page</a>      <br>      <a href="add" >go the add2 page</a>      </body>      </html>  

5、delete.jsp

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

6、add.jsp

    <span style="font-size:12px;"><%@ page language="java" contentType="text/html; charset=UTF-8"          pageEncoding="UTF-8"%>      <%@taglib prefix="s" uri="/struts-tags" %>      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">      <html>      <head>      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">      <title>Insert title here</title>      </head>      <body>      <p><s:property value="info"/></p>      </body>      </html>    </span>