【示例】Spring和Struts整合

此例子來自于《JavaEE企業(yè)應(yīng)用實戰(zhàn)》(李剛)

需要先導(dǎo)入spring和struts的jar包,一定要導(dǎo)入struts2-spring-plugin-版本號.jar,這是讓spring和struts相關(guān)聯(lián)的包。接下來是例子

struts.xml(在src文件下)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <package name="default" namespace="/" extends="struts-default">
        <!-- 此處的class并不是一個類,而是一個bean的id -->
        <action name="login" class="loginAction">
            <result name="error">error.jsp</result>
            <result>success.jsp</result>
        </action>
        <action name="*">
            <result>{1}.jsp</result>
        </action>
    </package>
</struts>

web.xml(WEB-INF文件夾下)

<?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" 
    id="WebApp_ID" version="3.1">
  <display-name>TestStruts5</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>
    <!-- ContextLoaderListener監(jiān)聽類實現(xiàn)了ServletContextListener接口,
         它可以在創(chuàng)建時自動查找WEB-INF文件夾下的applicationContext.xml文件
         如果要加載多個配置文件可以使用context-param對contextConfigLocation
         的參數(shù)進行配置
     -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <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>/*</url-pattern>
    </filter-mapping>
 
</web-app>

applicationContext.xml(在WEB-INF文件夾下)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <bean id="myService" class="com.service.impl.MyServiceImpl"/>
    <bean id="loginAction" class="com.action.LoginAction" scope="prototype" p:myService-ref="myService"/>
</beans>

<font color="red">注意:</font>對Action配置一定要加scope屬性,因為Action包含了請求的狀態(tài)信息,必須為每個請求對應(yīng)一個Action,所以不能將該Action實例配置成singleton行為

MyService接口

package com.service;

public interface MyService {
    int validLogin(String username , String pass);
}

MyService接口實現(xiàn)類

package com.service.impl;

import com.service.MyService;

public class MyServiceImpl implements MyService {

    @Override
    public int validLogin(String username, String pass) {
        // TODO Auto-generated method stub
        if(username.equals(pass)) {
            return 99;
        }
        return -1;
    }

}

Action類

package com.action;

import com.opensymphony.xwork2.ActionSupport;
import com.service.MyService;

public class LoginAction extends ActionSupport {
    private String username;
    private String password;
    private MyService myService;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setMyService(MyService myService) {
        this.myService = myService;
    }
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if(myService.validLogin(username, password) > 0) {
            return SUCCESS;
        }
        return ERROR;
    }
}

這里的myService只需要加set方法以便依賴注入。


接下來是測試用的頁面
index.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>
<s:form action="login">
    <s:textfield name="username" label="用戶名"/>
    <s:textfield name="password" label="密碼"/>
    <s:submit value="提交"/>
</s:form>
</body>
</html>

還需要一個success.jsp和error.jsp頁面來代表action相應(yīng)是成功還是失敗的,我這里I就不寫出來了。

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

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

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • (一)Struts、Spring、Hibernate、Mybatis框技術(shù) 1.Struts2.0有幾種標(biāo)簽庫 【...
    獨云閱讀 3,379評論 0 62
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,728評論 18 399
  • 烏龍古道位于浙江湖州長興縣小浦鎮(zhèn),北起八都岕(kǎ),南至周吳岕(kǎ),全長20余公里,主要翻越方山。 相傳16...
    無色生香閱讀 3,649評論 37 68

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