【示例】Spring和Struts整合

此例子來自于《JavaEE企業應用實戰》(李剛)

需要先導入spring和struts的jar包,一定要導入struts2-spring-plugin-版本號.jar,這是讓spring和struts相關聯的包。接下來是例子

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監聽類實現了ServletContextListener接口,
         它可以在創建時自動查找WEB-INF文件夾下的applicationContext.xml文件
         如果要加載多個配置文件可以使用context-param對contextConfigLocation
         的參數進行配置
     -->
    <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包含了請求的狀態信息,必須為每個請求對應一個Action,所以不能將該Action實例配置成singleton行為

MyService接口

package com.service;

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

MyService接口實現類

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相應是成功還是失敗的,我這里I就不寫出來了。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容