- 復制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
- ============================================
- 配置applicationContext.xml,并配置LoginAction的bean
- 普通bean配置,注意加上scope="prototype"
- 注解方式配置:<context:component-scan base-package="org.skzr.demo"/>
applicationContext.xml:添加注解到LoginAction.java,注意:@Component("loginActionComponent") @Scope("prototype")- <?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>
- /**
- * Copyright (c) 2010-2020 by wasion.com
- * All rights reserved.
- * @author <a href="mailto:skzr.org@gmail.com">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:skzr.org@gmail.com">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";
- }
- }
- 修改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>
- 啟動web查看后臺輸出是不是正常初始化spring
- 修改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>
- 哈哈struts運行不正常了,無法使用spring的:
- 修改struts.properties添加spring支持:struts.objectFactory=spring
- 復制struts2-spring-plugin-2.2.3.jar到lib,并添加到java build path
- 重新運行系統即可正常登錄了。
新的登錄頁面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>密 碼:</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>密 碼:</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>密 碼:</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>