彩虹天堂
          技術源于生活
          posts - 0,  comments - 2,  trackbacks - 0
          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>


                   五、測試(全部成功)    
              

                     



                  
                      
                      


                      
                      

          posted on 2008-09-17 12:42 bcterry 閱讀(215) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿

          文章檔案

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 彝良县| 剑川县| 启东市| 盖州市| 女性| 上栗县| 项城市| 兴义市| 庆安县| 尼玛县| 霸州市| 武夷山市| 大关县| 长治县| 印江| 通化市| 绥棱县| 磐安县| 明光市| 阿尔山市| 平塘县| 正阳县| 鹿邑县| 临猗县| 阿坝县| 罗田县| 高平市| 屏山县| 姜堰市| 民丰县| 郑州市| 伊吾县| 永兴县| 新竹市| 东丰县| 长垣县| 凤庆县| 楚雄市| 哈密市| 临夏县| 互助|