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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34108067/article/details/70340025

由于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>


展开阅读全文

没有更多推荐了,返回首页