5.6 Spring與Struts 2整合應用

html

開發一個Spring與Struts 2的整合項目的步驟以下。
java

1 建立Web項目Struts_Spring

2 添加Struts 2框架

添加Struts 2框架支持web

配置web.xml,代碼以下:
spring

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 <display-name>Struts_Spring</display-name>
 <filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.action</url-pattern>
 </filter-mapping>
 <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.jsp</url-pattern>
 </filter-mapping>
</web-app>

3 建立login.jsp

login.jsp代碼以下:apache

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
    <title>登陸界面</title>
</head>
<body>
<s:form action="login.action" method="post">
    <s:textfield name="xh" label="學號"/>
    <s:password name="kl" label="口令"/>
    <s:submit value="登陸"/>
    </s:form>
</body>
</html>

4 建立Action

LoginAction.java代碼以下:app

package org.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
  private String xh;
  private String kl;
  public String getXh() {
    return xh;
  }
  public void setXh(String xh) {
    this.xh = xh;
  }
  public String getKl() {
    return kl;
  }
  public void setKl(String kl) {
    this.kl = kl;
  }
  public String execute() throws Exception {
    return SUCCESS;
  }
}

配置struts.xml文件,代碼以下:
框架

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   <include file="struts-default.xml"/>
   <package name="default" extends="struts-default" namespace="/">
       <action name="login" class="org.action.LoginAction">
           <result name="success">/login_success.jsp</result>
       </action>
   </package>
</struts>

5 建立login_success.jsp

代碼以下:jsp

<%@ page contentType="text/html;charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body >
<h2>您好! <s:property value=" xh"/>歡迎您登陸成功 </h2>
</body>
</html>

6 部署運行

部署,測試Struts 2是否正常。運行http://localhost:8080/Struts_Spring/login.jsp,在登陸框和密碼框中任意輸入,單擊【登陸】按鈕,轉向登陸成功界面,並輸出登陸名。post

7 添加Spring框架

步驟同5.2.2節測試

8 添加Spring支持包struts2-spring-plugin.jar

注意,必定要加入該包,該包位於Struts 2的lib目錄下。

9 修改web.xml內容

修改web.xml內容,使得程序增長對Spring的支持。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 <display-name>Struts_Spring</display-name>
 <filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.action</url-pattern>
 </filter-mapping>
 <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.jsp</url-pattern>
 </filter-mapping>
 <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
</web-app>

10 建立消息包文件struts.properties

        在src文件夾下建立struts.properties文件,使得Struts 2的類的生成交給Spring完成。步驟以下:右擊項目的src文件夾,在彈出的菜單中選擇【New】→【File】菜單項,以後在Enter or select the parent folder中輸入struts2_spring/src,在File name欄中寫入struts.properties,單擊【肯定】按鈕。文件內容以下:

struts.objectFactory=spring


11 修改applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="loginAction" class="org.action.LoginAction"></bean>
</beans>


12 修改struts.xml

使得struts 2的類的生成交給Spring完成。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <include file="struts-default.xml"/>
    <package name="default" extends="struts-default" namespace="/">
        <action name="login" class="loginAction">
            <result name="success">/login_success.jsp</result>
        </action>
    </package>
</struts>


13 部署運行


附:目錄《JavaEE基礎實用教程》筆記說明

相關文章
相關標籤/搜索