[web工程]集成spring3

 

返回:[web工程]Struts2+Spring3.1+Hibernate3.6的集成

上一步:[web工程]集成struts2 mvc

下一步:[web工程]集成hibernate

集成spring3

  1. 复制jar到WEB-INF/lib目录:
    复制,并添加到java build path:
    org.springframework.aop-3.1.0.M1.jar
    org.springframework.asm-3.1.0.M1.jar
    org.springframework.beans-3.1.0.M1.jar
    org.springframework.context-3.1.0.M1.jar
    org.springframework.core-3.1.0.M1.jar
    org.springframework.expression-3.1.0.M1.jar
    org.springframework.web-3.1.0.M1.jar
     
    spring包分类总结:
    ============================================
    spring3 lib:
    	core:
    		org.springframework.beans-3.0.5.RELEASE.jar
    		org.springframework.core-3.0.5.RELEASE.jar
    		org.springframework.context-3.0.5.RELEASE.jar
    		org.springframework.expression-3.0.5.RELEASE.jar
    	  	core depend lib:
    			org.springframework.asm-3.0.5.RELEASE.jar
    	web:
    		tld: spring.tld, spring-form.tld
    		org.springframework.web-3.0.5.RELEASE.jar
    		springMVC: org.springframework.web.servlet-3.0.5.RELEASE.jar
    	db:
    		org.springframework.orm-3.0.2.RELEASE.jar
            org.springframework.transaction-3.0.2.RELEASE.jar
            org.springframework.jdbc-3.0.2.RELEASE.jar
    	aop:
    		org.springframework.aop-3.0.5.RELEASE.jar
    		depend on:
    			aopalliance-1.0.jar
    ============================================
     
  2. 配置applicationContext.xml,并配置LoginAction的bean
    1. 普通bean配置,注意加上scope="prototype"
    2. 注解方式配置:<context:component-scan base-package="org.skzr.demo"/>
      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" xmlns:aop="http://www.springframework.org/schema/aop"
      	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
      	xsi:schemaLocation="
      		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
      	<bean id="loginAction" class="org.skzr.demo.action.LoginAction" scope="prototype"/>
      	
      	<context:component-scan base-package="org.skzr.demo"/>
      </beans>
      
       添加注解到LoginAction.java,注意:@Component("loginActionComponent") @Scope("prototype")
      /**
       * Copyright (c) 2010-2020 by wasion.com
       * All rights reserved.
       * @author <a href="mailto:[email protected]">skzr.org</a>
       * @date 2011-5-19 下午10:20:57
       */
      package org.skzr.demo.action;
      
      import org.springframework.context.annotation.Scope;
      import org.springframework.stereotype.Component;
      
      import com.opensymphony.xwork2.ActionSupport;
      
      /**
       * 登录检测
       * @author <a href="mailto:[email protected]">skzr.org</a>
       * @version 1.0.0
       * @since JDK1.6
       */
      @Component("loginActionComponent")
      @Scope("prototype")
      public class LoginAction extends ActionSupport {
      	private static final long serialVersionUID = 1L;
      	/** 用户名 */
      	private String userName;
      	/** 密码 */
      	private String password;
      	
      	/**
      	 * @return {@link #userName}
      	 */
      	public String getUserName() {
      		return userName;
      	}
      	/**
      	 * @param userName {@link #userName}
      	 */
      	public void setUserName(String userName) {
      		this.userName = userName;
      	}
      	/**
      	 * @return {@link #password}
      	 */
      	public String getPassword() {
      		return password;
      	}
      	/**
      	 * @param password {@link #password}
      	 */
      	public void setPassword(String password) {
      		this.password = password;
      	}
      
      	/**
      	 * 登录验证
      	 * @return 验证后页面视图
      	 */
      	public String check() {
      		return "admin".equals(userName) ? "welcome" : "success";
      	}
      }
      
       
  3. 修改web.xml
    <?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">
    	<description>我爱编程</description>
    	<display-name>我爱编程</display-name>
    	
    	<context-param>
    		<param-name>webAppRootKey</param-name>
    		<param-value>app.root</param-value>
    	</context-param>
    	<context-param>
    		<param-name>log4jRefreshInterval</param-name>
    		<param-value>10000</param-value>
    	</context-param>
    	
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			classpath*:applicationContext.xml
    		</param-value>
    	</context-param>
    	<listener>
    		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    	</listener>
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	
    	<filter>
    		<filter-name>struts2</filter-name>
    		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    		<init-param>
    			<param-name>config</param-name>
    			<param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>struts2</filter-name>
    		<url-pattern>*.do</url-pattern>
    	</filter-mapping>
    	
    	<welcome-file-list>
    		<welcome-file>index.jsp</welcome-file>
    	</welcome-file-list>
    </web-app>
    
     
  4. 启动web查看后台输出是不是正常初始化spring
  5. 修改struts.xml的action从spring中获取
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    	<package name="demo" extends="struts-default" namespace="/">
    		<action name="loginAction" class="org.skzr.demo.action.LoginAction">
    			<result>/WEB-INF/jsp/login.jsp</result>
    			<result name="welcome">/WEB-INF/jsp/welcome.jsp</result>
    		</action>
    		<!-- 来自spring配置的bean -->
    		<action name="loginActionSpring" class="loginAction">
    			<result>/WEB-INF/jsp/login.jsp</result>
    			<result name="welcome">/WEB-INF/jsp/welcome.jsp</result>
    		</action>
    		<!-- 来自spring注解配置的bean -->
    		<action name="loginActionSpringComponent" class="loginActionComponent">
    			<result>/WEB-INF/jsp/login.jsp</result>
    			<result name="welcome">/WEB-INF/jsp/welcome.jsp</result>
    		</action>
    	</package>
    </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
      <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
      <%
      String path = request.getContextPath();
      String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
      %>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      	<head>
      		<base href="<%=basePath%>">
      		<title>系统登录:</title>
      		<meta http-equiv="pragma" content="no-cache">
      		<meta http-equiv="cache-control" content="no-cache">
      		<meta http-equiv="expires" content="0">
      		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
      		<meta http-equiv="description" content="This is my page">
      	</head>
      
      	<body>
      		<center><h1>struts action</h1></center>
      		<form action="loginAction!check.do" method="post">
      			<table align="center">
      					<tr><td>用户名:</td><td><input name="userName" value="${userName }"></td></tr>
      					<tr><td>密&nbsp;码:</td><td><input name="password" type="password"></td></tr>
      					<tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>
      			</table>
      		</form>
      		
      		<center><h1>action配置在spring中的</h1></center>
      		<form action="loginActionSpring!check.do" method="post">
      			<table align="center">
      					<tr><td>用户名:</td><td><input name="userName" value="${userName }"></td></tr>
      					<tr><td>密&nbsp;码:</td><td><input name="password" type="password"></td></tr>
      					<tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>
      			</table>
      		</form>
      		
      		<center><h1>action配置在spring中的(注解方式配置)</h1></center>
      		<form action="loginActionSpringComponent!check.do" method="post">
      			<table align="center">
      					<tr><td>用户名:</td><td><input name="userName" value="${userName }"></td></tr>
      					<tr><td>密&nbsp;码:</td><td><input name="password" type="password"></td></tr>
      					<tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr>
      			</table>
      		</form>
      	</body>
      </html>
      
       
  7. 下载demo程序 Good luck ^ ^

返回:[web工程]Struts2+Spring3.1+Hibernate3.6的集成

上一步:[web工程]集成struts2 mvc

扫描二维码关注公众号,回复: 834375 查看本文章

下一步:[web工程]集成hibernate

猜你喜欢

转载自skzr-org.iteye.com/blog/1050929