Struts2.5版本以上的struts.xml配置
阿新 • • 發佈:2019-01-07
由於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檔案裡,如圖
最後我附上一個配置的例子
在這之前你需要下載如下一下struts包並載入到libraries裡,這些類可以去Apache官網下載
有有時候有些包需要放到tomcat的lib裡面才可以,不敢上述的包不需要
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
<?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>
這個如果嫌難打的話可以去原始碼包拷貝,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"%>
<!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
<%@ 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>
6、add.jsp
<%@ 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>