//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" id="WebApp_ID" version="3.1">
<display-name>Spring_struts2</display-name>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/spring/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,resources/struts2/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
</web-app>
注意:struts2配置文件默认存放路径在/WEB-INF/classes目录下,即将struts.xml放在src的目录下。
但是为了协作开发与方便管理,我们有时需要把struts.xml放到其他位置
struts2加载配置文件都是先从自己的jar包和/WEB-INF/classes两个默认的位置加载的。
若修改struts2配置文件的存放位置,在web.xml配置过虑器,具体配置如下:
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,
struts-plugin.xml,resources/struts2/struts.xml</param-value>
</init-param>
在这里我把struts.xml放在了src下的resources/struts2包下,因为设置了<param-name>config</param-name>参数,所以struts-default.xml,struts-plugin.xml等原来struts2默认加载的文件也需要手动指定,否则不会自动加载。
若不在这里配置struts-default.xml,struts-plugin.xml,也可在struts.xml文件中include将两个文件包含进来。
<include file="struts-default.xml" />
<include file="struts-plugin.xml" />
如若约定大于配置,多个子配置文件的话可以采用扫描的方式如:
<include file="com/home/conf/struts-*.xml" />
或直接
<include file="com/home/conf/*.xml" />
//index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
</head>
<body>
<form action="test/loginAction.action" method="post">
用户名:<input name="name" type="text" >
密码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
//success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<s:property value="name"/>,欢迎回来.
</body>
</html>
//fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<script type="text/javascript" src="/js/jquery-1.4.2.js"></script>
</head>
<body>
<script type="text/javascript">
alert('登录失败,请重试!');
window.location='../index.jsp';
</script>
</body>
</html>
运行结果: