posts - 241,  comments - 116,  trackbacks - 0
  1. 复制jar到WEB-INF/lib目录:
    复制,并添加到java build path:
    Sql代码  收藏代码
    1. org.springframework.aop-3.1.0.M1.jar  
    2. org.springframework.asm-3.1.0.M1.jar  
    3. org.springframework.beans-3.1.0.M1.jar  
    4. org.springframework.context-3.1.0.M1.jar  
    5. org.springframework.core-3.1.0.M1.jar  
    6. org.springframework.expression-3.1.0.M1.jar  
    7. org.springframework.web-3.1.0.M1.jar  
     
    spring包分类总结:
    Sql代码  收藏代码
    1. ============================================  
    2. spring3 lib:  
    3.     core:  
    4.         org.springframework.beans-3.0.5.RELEASE.jar  
    5.         org.springframework.core-3.0.5.RELEASE.jar  
    6.         org.springframework.context-3.0.5.RELEASE.jar  
    7.         org.springframework.expression-3.0.5.RELEASE.jar  
    8.         core depend lib:  
    9.             org.springframework.asm-3.0.5.RELEASE.jar  
    10.     web:  
    11.         tld: spring.tld, spring-form.tld  
    12.         org.springframework.web-3.0.5.RELEASE.jar  
    13.         springMVC: org.springframework.web.servlet-3.0.5.RELEASE.jar  
    14.     db:  
    15.         org.springframework.orm-3.0.2.RELEASE.jar  
    16.         org.springframework.transaction-3.0.2.RELEASE.jar  
    17.         org.springframework.jdbc-3.0.2.RELEASE.jar  
    18.     aop:  
    19.         org.springframework.aop-3.0.5.RELEASE.jar  
    20.         depend on:  
    21.             aopalliance-1.0.jar  
    22. ============================================  
     
  2. 配置applicationContext.xml,并配置LoginAction的bean
    1. 普通bean配置,注意加上scope="prototype"
    2. 注解方式配置:<context:component-scan base-package="org.skzr.demo"/>
      applicationContext.xml:
      Xml代码  收藏代码
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <beans xmlns="http://www.springframework.org/schema/beans"  
      3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
      4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
      5.     xsi:schemaLocation="  
      6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      7.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
      8.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
      9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
      10.   
      11.     <bean id="loginAction" class="org.skzr.demo.action.LoginAction" scope="prototype"/>  
      12.       
      13.     <context:component-scan base-package="org.skzr.demo"/>  
      14. </beans>  
       添加注解到LoginAction.java,注意:@Component("loginActionComponent") @Scope("prototype")
      Java代码  收藏代码
      1. /** 
      2.  * Copyright (c) 2010-2020 by wasion.com 
      3.  * All rights reserved. 
      4.  * @author <a href="mailto:skzr.org@gmail.com">skzr.org</a> 
      5.  * @date 2011-5-19 下午10:20:57 
      6.  */  
      7. package org.skzr.demo.action;  
      8.   
      9. import org.springframework.context.annotation.Scope;  
      10. import org.springframework.stereotype.Component;  
      11.   
      12. import com.opensymphony.xwork2.ActionSupport;  
      13.   
      14. /** 
      15.  * 登录检测 
      16.  * @author <a href="mailto:skzr.org@gmail.com">skzr.org</a> 
      17.  * @version 1.0.0 
      18.  * @since JDK1.6 
      19.  */  
      20. @Component("loginActionComponent")  
      21. @Scope("prototype")  
      22. public class LoginAction extends ActionSupport {  
      23.     private static final long serialVersionUID = 1L;  
      24.     /** 用户名 */  
      25.     private String userName;  
      26.     /** 密码 */  
      27.     private String password;  
      28.       
      29.     /** 
      30.      * @return {@link #userName} 
      31.      */  
      32.     public String getUserName() {  
      33.         return userName;  
      34.     }  
      35.     /** 
      36.      * @param userName {@link #userName} 
      37.      */  
      38.     public void setUserName(String userName) {  
      39.         this.userName = userName;  
      40.     }  
      41.     /** 
      42.      * @return {@link #password} 
      43.      */  
      44.     public String getPassword() {  
      45.         return password;  
      46.     }  
      47.     /** 
      48.      * @param password {@link #password} 
      49.      */  
      50.     public void setPassword(String password) {  
      51.         this.password = password;  
      52.     }  
      53.   
      54.     /** 
      55.      * 登录验证 
      56.      * @return 验证后页面视图 
      57.      */  
      58.     public String check() {  
      59.         return "admin".equals(userName) ? "welcome" : "success";  
      60.     }  
      61. }  
       
  3. 修改web.xml
    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
    6.     <description>我爱编程</description>  
    7.     <display-name>我爱编程</display-name>  
    8.       
    9.     <context-param>  
    10.         <param-name>webAppRootKey</param-name>  
    11.         <param-value>app.root</param-value>  
    12.     </context-param>  
    13.     <context-param>  
    14.         <param-name>log4jRefreshInterval</param-name>  
    15.         <param-value>10000</param-value>  
    16.     </context-param>  
    17.       
    18.     <context-param>  
    19.         <param-name>contextConfigLocation</param-name>  
    20.         <param-value>  
    21.             classpath*:applicationContext.xml  
    22.         </param-value>  
    23.     </context-param>  
    24.     <listener>  
    25.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    26.     </listener>  
    27.     <listener>  
    28.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    29.     </listener>  
    30.       
    31.     <filter>  
    32.         <filter-name>struts2</filter-name>  
    33.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    34.         <init-param>  
    35.             <param-name>config</param-name>  
    36.             <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>  
    37.         </init-param>  
    38.     </filter>  
    39.     <filter-mapping>  
    40.         <filter-name>struts2</filter-name>  
    41.         <url-pattern>*.do</url-pattern>  
    42.     </filter-mapping>  
    43.       
    44.     <welcome-file-list>  
    45.         <welcome-file>index.jsp</welcome-file>  
    46.     </welcome-file-list>  
    47. </web-app>  
     
  4. 启动web查看后台输出是不是正常初始化spring
  5. 修改struts.xml的action从spring中获取
    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
    3. <struts>  
    4.     <package name="demo" extends="struts-default" namespace="/">  
    5.         <action name="loginAction" class="org.skzr.demo.action.LoginAction">  
    6.             <result>/WEB-INF/jsp/login.jsp</result>  
    7.             <result name="welcome">/WEB-INF/jsp/welcome.jsp</result>  
    8.         </action>  
    9.         <!-- 来自spring配置的bean -->  
    10.         <action name="loginActionSpring" class="loginAction">  
    11.             <result>/WEB-INF/jsp/login.jsp</result>  
    12.             <result name="welcome">/WEB-INF/jsp/welcome.jsp</result>  
    13.         </action>  
    14.         <!-- 来自spring注解配置的bean -->  
    15.         <action name="loginActionSpringComponent" class="loginActionComponent">  
    16.             <result>/WEB-INF/jsp/login.jsp</result>  
    17.             <result name="welcome">/WEB-INF/jsp/welcome.jsp</result>  
    18.         </action>  
    19.     </package>  
    20. </struts>      
     
  6. 哈哈struts运行不正常了,无法使用spring的:
    1. 修改struts.properties添加spring支持:struts.objectFactory=spring
    2. 复制struts2-spring-plugin-2.2.3.jar到lib,并添加到java build path
    3. 重新运行系统即可正常登录了。
      新的登录页面login.jsp
      Html代码  收藏代码
      1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
      2. <%  
      3. String path = request.getContextPath();  
      4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
      5. %>  
      6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
      7. <html>  
      8.     <head>  
      9.         <base href="<%=basePath%>">  
      10.         <title>系统登录:</title>  
      11.         <meta http-equiv="pragma" content="no-cache">  
      12.         <meta http-equiv="cache-control" content="no-cache">  
      13.         <meta http-equiv="expires" content="0">  
      14.         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
      15.         <meta http-equiv="description" content="This is my page">  
      16.     </head>  
      17.   
      18.     <body>  
      19.         <center><h1>struts action</h1></center>  
      20.         <form action="loginAction!check.do" method="post">  
      21.             <table align="center">  
      22.                     <tr><td>用户名:</td><td><input name="userName" value="${userName }"></td></tr>  
      23.                     <tr><td>密&nbsp;码:</td><td><input name="password" type="password"></td></tr>  
      24.                     <tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>  
      25.             </table>  
      26.         </form>  
      27.           
      28.         <center><h1>action配置在spring中的</h1></center>  
      29.         <form action="loginActionSpring!check.do" method="post">  
      30.             <table align="center">  
      31.                     <tr><td>用户名:</td><td><input name="userName" value="${userName }"></td></tr>  
      32.                     <tr><td>密&nbsp;码:</td><td><input name="password" type="password"></td></tr>  
      33.                     <tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>  
      34.             </table>  
      35.         </form>  
      36.           
      37.         <center><h1>action配置在spring中的(注解方式配置)</h1></center>  
      38.         <form action="loginActionSpringComponent!check.do" method="post">  
      39.             <table align="center">  
      40.                     <tr><td>用户名:</td><td><input name="userName" value="${userName }"></td></tr>  
      41.                     <tr><td>密&nbsp;码:</td><td><input name="password" type="password"></td></tr>  
      42.                     <tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>  
      43.             </table>  
      44.         </form>  
      45.     </body>  
      46. </html>  
       
posted on 2011-05-20 14:58 墙头草 阅读(820) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
人人游戏网 软件开发网 货运专家