struts2整合spring應用實例

我們知道struts1與spring整合是靠org.springframework.web.struts.DelegatingActionProxy來實現的,以下通過具體一個用戶登錄實現來說明struts2整合spring的相關內容.

    一、準備工作
        

 1.實例分析我們在這不與數據庫打交道,所有就是當用登錄的時候判斷用戶名是否爲指定值,密碼是否爲指定值,以及相關的異常處理、
        2.爲什麼我們要說struts2整合spring呢?相信在家都知道,我也不用多說了....
        4.在  http://struts.apache.org/download.cgi#struts212下載struts2的jar包,源碼,API文檔.
        5.在  http://static.springframework.org/downloads/nightly/release-download.php下載不同版本的spring的jar包,源碼,API文檔.
        6.配置開發環境:MyEclipse6.0+Eclipse3.3+JDK6.0+Tomcat6.0+Struts 2.0.11+spring2.0。
    7.新建web項目,導入相應的jar包,如以下所示:
     a.由於現在IDE開發工具還沒有對struts2.0有很好的支持,所有我們需要手功配置,首先將我們剛下下來的struts2.0的lib裏面的commons-logging-1.0.4.jar、ognl-2.6.11.jar、xwork-2.0.4.jar、freemarker-2.3.8.jar、struts2-core-2.0.11.1.jar以及struts2.0裏面所需要的插件包struts2-spring-plugin-2.0.11.1.jar添加的WEB-INF/lib下面
     b.通過通過IDE開發工具項目對spring2.0的支持
    7.在src下建立struts.xml文件(具體的寫法在後面呈現)
    8.配置web.xml,如下:
    

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd"
>
    
    
    <!-- 加載struts2核心 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 指明spring配置文件在何處 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>

    <!-- 加載spring配置文件applicationContext.xml -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>    
</web-app>


        

    二、建立前臺頁面
      1.用戶登錄肯定有一個用戶登錄頁面login.jsp,如下:
        

<%@ page language="java"  pageEncoding="GB2312"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
  <head>
      <title>login2</title>
  </head>

  <body>
      <s:form name="login" action="login" method="post" >
          <s:textfield name="username" label="帳號"></s:textfield>
          <s:password name="password"  label="密碼"></s:password>
          <s:submit></s:submit>
      </s:form>
  </body>
</html>

   2.若登錄成功的index.jsp文件,如下:
    

<%@ page language="java"  pageEncoding="GB2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
  <head>
      <title>login2</title>
  </head>
    
  <body>
          <div>
              <h4>歡迎你!</h4><font color="red">${username}</font>
              <br/>
              <h4>你登錄的密碼是<h4><font color="red">${password}</font>;
          </div>
  </body>
</html>

3、用戶名非法提示頁面.usernameInvalid.jsp(通過el表達示得到異常信息)
    
    

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    用戶名非法:<font color="red">${exception.message}</font>
</body>
</html>

4、密碼非法提示頁面.passwordInvalid.jsp(struts2標籤得到異常信息);
    
    

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
 <%@ 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=GB18030">
<title>Insert title here</title>
</head>
<body>
    密碼非法:<FONT  color="red"><s:property value="exception.message"/></FONT>
</body>
</html>

    
     三、建立對應的action

       1.提供用戶請求服務的LoginService類
       
           

package org.topCSA.s2s.service;

import org.topCSA.s2s.exception.PasswordException;
import org.topCSA.s2s.exception.UsernameException;

public class LoginService
{
    /*
     * 我們這只是一個小的例子,不與數據庫打交到
     * 若有數據庫操作,那麼在這個LoginService就是操作具體Dao類實現登錄的相關操作
     
*/

    public boolean validate(String username,String password)throws Exception
    {
        boolean v = false;
        if(!"xcp".equals(username))//如果用戶名不等於xcp,就拋出一個異常
        {
            throw new UsernameException("用戶名不正確");
        }

        else if(!"123".equals(password))))//如果密碼不等於123,就拋出一個異常

        {
            throw new PasswordException("密碼不正確");
        }

        else
        {
            v = true;            
        }

        return v;
    }

}


       2.接收用戶請求的LoginAction類

        

package org.topCSA.s2s.action;

import org.topCSA.s2s.service.LoginService;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{

    /**
     * 
     
*/

    private static final long serialVersionUID = 1L;

    private String username;
    private String password;
    
    /*
     * 我們通過Spring的IOC容器注入LoginService,從而減少類之間的依賴關係
     
*/

    private LoginService loginService;
    
    public LoginService getLoginService()
    {
        return loginService;
    }

    public void setLoginService(LoginService loginService)
    {
        this.loginService = loginService;
    }

    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;
    }

    
    
    @Override
    public void validate()
    {
        /*
         * 我們可以在這個方法類加一些輸入驗證 但是爲了體現後面我們寫的業務邏輯方法這就不驗證
         
*/

    }

    
    @Override
    public String execute() throws Exception
    {
        
        boolean result = loginService.validate(username, password);
        if(result == true)
        {
            return SUCCESS;
        }

        else
        {
            return INPUT;
        }

    }

}

   
  四、配置struts.xml與applicationContext.xml
    
        1.配置struts.properties,爲了解決中文問題,具體用法參照struts2的用法如下:裏面加上struts.i18n.encoding = gb2312,當然也可以直接加到struts.xml裏面寫法爲<constant name="struts.i18n.encoding" value="gbk"></constant>
        2.配置struts.xml
        

<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="struts2" extends="struts-default">
        <action name="login" class="LoginAction">
            <exception-mapping result="usernameInvalid" exception="org.topCSA.s2s.exception.UsernameException" />
            <exception-mapping result="passwordInvalid" exception="org.topCSA.s2s.exception.PasswordException" />
            <result name="success">/index.jsp</result>
            <result name="input">/login.jsp</result>
            <result name="usernameInvalid">/usernameInvalid.jsp</result>
            <result name="passwordInvalid">/passwordInvalid.jsp</result>
        </action>
    </package>
</struts>


        3.配置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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
    
<bean name="loginService" class="org.topCSA.s2s.service.LoginService" />
    
    
<bean name="LoginAction" class="org.topCSA.s2s.action.LoginAction">
        
<property name="loginService">
            
<ref bean="loginService"/>
        
</property>
    
</bean>        

</beans>

補充exception的代碼:

public class UsernameException extends Exception {

/**

*/
private static final long serialVersionUID = 1L;

public UsernameException(){};
public UsernameException(String message)
{
super(message);
}

}

public class PasswordException extends Exception {

/**

*/
private static final long serialVersionUID = 1L;
public PasswordException(){}
public PasswordException(String message)
{
super(message);
}

}

         五、測試(全部成功)    
    

           



        
            
            


            
            

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章