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

*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文件里,如图
Struts2.5版本以上的struts.xml和jar包配置_第1张图片

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

所需jar包
Struts2.5版本以上的struts.xml和jar包配置_第2张图片

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

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

      
    <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>Strutsdisplay-name>  
        <welcome-file-list>  
            <welcome-file>index.htmlwelcome-file>  
            <welcome-file>index.htmwelcome-file>  
            <welcome-file>index.jspwelcome-file>  
            <welcome-file>default.htmlwelcome-file>  
            <welcome-file>default.htmwelcome-file>  
            <welcome-file>default.jspwelcome-file>  
        welcome-file-list>  
        <filter>  
            <filter-name>Struts2filter-name>  
            <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>  
        filter>  
        <filter-mapping>  
            <filter-name>Struts2filter-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;">  

      

    <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.jspresult>  
                <result name="add">add.jspresult>  
                <result name="delete">delete.jspresult>  
            action>  

        package>  
    struts>span>  

注意class指定是action的类名

3、UserAction类

import com.opensymphony.xwork2.ActionSupport
public 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"%>  
      
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>Insert title heretitle>  
    <link rel="stylesheet" type= "text/css" href="css/indescss.css">  
    head>  

    <body>  

    <%String path = request.getContextPath(); %>  
    <a href="first!add" >go the add pagea>  
    <br>  
    <a href="first!delete" >go the delete pagea>  
    <br>  
    <a href="first.action" >go the first pagea>  
    <br>  
    <a href="add" >go the add2 pagea>  
    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" %>  
  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>Insert title heretitle>  
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" %>  
      
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>Insert title heretitle>  
    head>  
    <body>  
    <p><s:property value="info"/>p>  
    body>  
    html>
    span>  

你可能感兴趣的:(框架)