<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    J2EE社區

    茍有恒,何必三更起五更眠;
    最無益,只怕一日曝十日寒.
    posts - 241, comments - 318, trackbacks - 0, articles - 16

    struts2整合spring應用實例

    Posted on 2008-09-13 17:34 xcp 閱讀(54255) 評論(22)  編輯  收藏 所屬分類: struts2
        我們知道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>


             五、測試(全部成功)    
        

               



            
                
                


                
                




    名稱: ?4C.ESL | .↗Evon
    口號: 遇到新問題?先要尋找一個方案乄而不是創造一個方案こ
    mail: 聯系我


    Feedback

    # re: struts2整合spring應用實例  回復  更多評論   

    2008-09-13 18:48 by 12
    ?你的exception呢

    # re: struts2整合spring應用實例  回復  更多評論   

    2008-09-15 13:37 by xcp
    @12
    呵呵,exception我寫在局部的!!!

    # re: struts2整合spring應用實例  回復  更多評論   

    2009-02-18 14:32 by mary
    想看
    exceptn.PasswordException和
    UsernameException的代碼

    # re: struts2整合spring應用實例  回復  更多評論   

    2010-03-23 13:12 by 打醬油的
    你的exception讓我們看看可以嗎

    # re: struts2整合spring應用實例  回復  更多評論   

    2011-06-21 15:40 by 李俊
    寫的很清晰, 初學者或是想快速掌握的人的理想資料.

    # re: struts2整合spring應用實例[未登錄]  回復  更多評論   

    2011-12-28 10:34 by JAVA
    補充exception的代碼

    public class UsernameException extends Exception {

    /**
    *
    */
    private static final long serialVersionUID = 1L;

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

    }

    # re: struts2整合spring應用實例[未登錄]  回復  更多評論   

    2011-12-28 10:35 by JAVA
    另外一個exception的代碼
    public class PasswordException extends Exception {

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

    }

    # re: struts2整合spring應用實例[未登錄]  回復  更多評論   

    2011-12-31 00:53 by 小豬
    謝謝

    # re: struts2整合spring應用實例[未登錄]  回復  更多評論   

    2012-02-23 23:06 by 123
    多謝博主 我也成功了

    # re: struts2整合spring應用實例  回復  更多評論   

    2012-09-09 12:24 by lihuabest
    多謝,初學者,整合不容易??!

    # re: struts2整合spring應用實例  回復  更多評論   

    2013-01-22 09:46 by wangsk
    謝謝樓主啊

    # re: struts2整合spring應用實例  回復  更多評論   

    2013-03-30 15:20 by 王保蛟
    樓主,我按照你這個做了。提示找不到LoginAction,我用的spring3.0

    # re: struts2整合spring應用實例[未登錄]  回復  更多評論   

    2013-04-02 20:03 by 小寶
    很清晰 感謝樓主

    # re: struts2整合spring應用實例[未登錄]  回復  更多評論   

    2013-06-02 19:24 by lee
    奇怪了,你們竟然都能成功?我也是按照樓主的寫法一步一步的寫的,后來這里始終有報這錯:
    com.lee.ssh.test.exception.PasswordException: 密碼不正確
    com.lee.ssh.test.login.LoginService.validate(LoginService.java:13)
    com.lee.ssh.test.login.LoginAction.execute(LoginAction.java:29)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    ..................

    求解……

    # re: struts2整合spring應用實例  回復  更多評論   

    2013-07-29 22:24 by lcc0210
    Struts2的action是每一此請求都被實例化一次,你用spring注入LoginService保存的是非靜態變量,當訪問action時action又被實例化了一次,此時LoginService已經不是現在這個對象所擁有,還是null。

    # re: struts2整合spring應用實例  回復  更多評論   

    2013-11-05 14:12 by 投幣手
    @lee
    你應該是沒寫這兩個jsp頁面
    passwordInvalid.jsp和usernameInvalid.jsp

    # re: struts2整合spring應用實例  回復  更多評論   

    2013-11-27 21:23 by jackLin
    spring中的bean管理struts中的action時是要配置scope的

    # re: struts2整合spring應用實例  回復  更多評論   

    2014-05-04 09:47 by 濤濤
    非常感謝

    # re: struts2整合spring應用實例  回復  更多評論   

    2014-10-27 15:42 by 123456
    嚴重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4909)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    十月 27, 2014 3:44:05 下午 org.apache.catalina.core.StandardContext listenerStart
    嚴重: Skipped installing application listeners due to previous error(s)

    # re: struts2整合spring應用實例  回復  更多評論   

    2014-10-29 15:09 by 合格
    根本不能運行

    # re: struts2整合spring應用實例  回復  更多評論   

    2016-01-25 10:22 by 落念
    為什么我的是404錯誤

    # re: struts2整合spring應用實例  回復  更多評論   

    2016-02-28 11:58 by hdblocal
    action中注入不了service
    主站蜘蛛池模板: 苍井空亚洲精品AA片在线播放 | 亚洲av午夜精品无码专区| 国产亚洲一区二区在线观看| 亚洲精品无码久久毛片| 亚洲精品专区在线观看| 亚洲国产香蕉人人爽成AV片久久 | 日韩中文字幕精品免费一区| 国产精品免费精品自在线观看| 18女人毛片水真多免费| 日韩精品免费一级视频| 我们的2018在线观看免费高清| 国产92成人精品视频免费| 在线观看特色大片免费视频| 午夜高清免费在线观看| 免费国产在线观看不卡| 中文字幕日韩亚洲| 亚洲丁香色婷婷综合欲色啪| 亚洲欧洲精品在线| 亚洲欧美日韩一区二区三区| 黄色免费网址在线观看| 中文字幕成人免费高清在线视频 | 一区二区三区免费视频观看| 伊人免费在线观看高清版| 伊人久久免费视频| 久久久www成人免费毛片| 国产免费拔擦拔擦8x| 精品亚洲成α人无码成α在线观看| 亚洲成AV人片在线观看无码| 亚洲经典在线中文字幕| 亚洲精品中文字幕| 国产精品视频全国免费观看 | 黄页视频在线观看免费| 国产一二三四区乱码免费| 亚洲人成在线免费观看| 精品久久久久久久免费加勒比| 久久亚洲2019中文字幕| 亚洲精品日韩专区silk| 鲁死你资源站亚洲av| 免费视频精品一区二区三区| 久久不见久久见中文字幕免费| 国产日韩成人亚洲丁香婷婷|