spring整合struts2(續)

weixin_33912445發表於2017-02-22
//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>

執行結果:

2352668-bea4f2ddb7895ad8.png
result

相關文章