spring整合struts2(續(xù))

//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的目錄下。
但是為了協(xié)作開發(fā)與方便管理,我們有時需要把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>參數(shù),所以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>

運行結(jié)果:

result
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評論 19 139
  • 概述 什么是Struts2的框架Struts2是Struts1的下一代產(chǎn)品,是在 struts1和WebWork的...
    inke閱讀 2,345評論 0 50
  • 本文包括: 1、Struts 2 概述2、Struts 2 快速入門3、Struts 2 的執(zhí)行流程4、配置 st...
    廖少少閱讀 3,215評論 3 13
  • 1.什么是框架 什么是框架,框架從何而來,為什么使用框架? 1).框架(framework)——半成品: 1.是一...
    賈里閱讀 3,509評論 0 0
  • 墨染江湖(1)初入江湖 莫小墨答應陳晨愿意加入他組建的武術隊伍后匆忙離開了導演室,卻發(fā)現(xiàn)自己忘記了回酒店的路但又羞...
    愛學習的荔枝閱讀 383評論 0 3

友情鏈接更多精彩內(nèi)容