struts2和spring3.2的整合 詳細演示


1、首先我們新建一個Web工程,如下:

 

2、導入Spring和Struts2的jar包。

 

其中,struts2-spring-plugin-2.1.8.jar是struts2、spring整合的關鍵。

3、首先新建一個業務代碼LoginAction,演示登錄處理。

package action;

import server.MyServer;
import server.MyServerImpl;

import com.opensymphony.xwork2.Action;

public class LoginAction implements Action {

    private String username;
    private String password;
    private String tip;
    private MyServer ms;

    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 String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }

    public void setMs(MyServer ms) {
        this.ms = ms;
    }

    public String execute() throws Exception {
        //setMs(new MyServerImpl());
        if (ms.valid(getUsername(), getPassword())) {
            setTip("登錄成功");
            return "success";
        } else {
            return "error";
        }
    }

}

4、然后新建一個接口MyServer,如下:

package server;

public interface MyServer {

    public boolean valid(String username,String password);
    
}

5、然后新建一個實現類,如下:(這里為了演示方便,沒有分包)

package server;

public class MyServerImpl implements MyServer {

    public boolean valid(String username, String password) {
        if(username.equals("cat")&&password.equals("123")){
            return true;
        }
        return false;
    }

}

6、在web.xml文件中對struts2和spring進行配置,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>
 
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   
</web-app>

7、在WebRoot中的WEB-INF下新建一個applicationContext.xml文件,配置spring,如下:

(注意,這個文件不能直接在src下配置,必須在這里配置,不然web容器找不到)

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <bean id="myServer" class="server.MyServerImpl"></bean>
    <bean id="loginAction" class="action.LoginAction" scope="prototype">
        <property name="ms" ref="myServer"></property>
    </bean>
</beans>

8、然后在src下新建一個struts.xml,配置struts2,如下:

(注意文件中action的class屬性,不是一個類,而是spring配置中bean的id,屬性由spring來注入)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    <package name="login" namespace="/login" extends="struts-default">

        <action name="loginPro" class="loginAction">
            <result name="success">
                /WEB-INF/content/welcome.jsp
            </result>
            <result name="error">
                /WEB-INF/content/error.jsp
            </result>
        </action>
        <!-- 讓用戶直接訪問該應用時列出所有視圖頁面 -->
        <action name="*">
            <result name="success">/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>

9、至此,基本配置完畢,再加上三個視圖文件login.jsp、welcome.jsp、error.jsp,如下:

login.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>登錄</title>
</head>
<body>
    <form action="loginPro" method="post">
    <table>
        <caption>用戶登錄</caption>
        <tr>
            <td>用戶名:</td>
            <td><input type="text" name="username"/></td>
        </tr>
        <tr>
            <td>密碼:</td>
            <td><input type="text" name="password"/></td>
        </tr>
        <tr>
            <td><input value="提交" type="submit"/></td>
            <td><input value="重置" type="reset"/></td>
        </tr>
    </table>
    </form>
</body>
</html>
welcome.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=ISO-8859-1">
        <title>welcome</title>
    </head>
    <body>
        這是歡迎頁面。
        <s:property value="tip" />    
    </body>
</html>
error.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    出現錯誤啦!
</body>
</html>

10、整個工程結構如下:

 

11、測試如下:

啟動服務器,地址欄輸入:http://127.0.0.1:8080/Struts2AndSpring/login/login

頁面如下:

 

輸入用戶名和密碼:

   

提交后如下:

   

參數傳遞正確。

輸入錯誤用戶名和密碼:

 

顯示如下:

 

 

至此,演示成功。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM