在spring2.5整合struts1.2的基础上,我们再把spring2.5和struts2.1.8整合一下.
首先,还是导包,我使用的包如下:commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
struts2-pell-multipart-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-logging.jar
spring.jar
struts2-spring-plugin-2.1.8.1.jar
spring-webmvc-struts.jar
接下现建立jsp页面:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <html>
- <head>
- <title> 登录页面 </title>
- <link rel=stylesheet href="css/login.css" type="text/css">
- </head>
- <body>
- <h1 align="center">用户登录页面</h1>
- <hr>
- <div align="center">
- <form action="login.lp" method="post">
- <table cellspacing=5 border=5 bodercolor=#ffaa00 >
- <tr><th colspan="3" align="center" bgcolor=#ffaa00>用户登录</th></tr>
- <tr>
- <th rowspan="3" background="images/2.jpg" style="width=90px"></th>
- <td>用户名:</td><td><input type="text" class="message" name="username"></td></tr>
- <tr><td>密 码:</td><td><input class="message" type="password" name="userpassword"></td></tr>
- <tr><td colspan="2" align="center"><input type="submit" value="登录"> <input type="reset" value="重置"></td></tr>
- </table>
- </form>
- </div>
- </body>
- </html>
成功跳转页面和失败跳转页面:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <html>
- <head>
- <title>Succsess</title>
- </head>
- <!--默认把值放在了request作用域里面,如果要用session,则需要另外的方法 -->
- <body>
- <p> Succsess.${username } ${userpassword }</p><br>
- <a href="exit.jsp">退出系统</a>
- </body>
- </html>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <html>
- <head>
- <title>Failure</title>
- </head>
- <!--默认把值放在了request作用域里面,如果要用session,则需要另外的方法 -->
- <body>
- <p> Failure.${username } ${userpassword }</p><br>
- <a href="exit.jsp">退出系统</a>
- </body>
- </html>
接着写业务类和action类
- public interface LoginService {
- public void check(String username,String userpassword);
- }
- package myclass.service.impl;
- import myclass.service.LoginService;
- public class LoginServiceImp implements LoginService{
- public void check(String username, String userpassword) {
- System.out.println("欢迎你来到业务层:"+username+"---"+userpassword);
- }
- }
- package myclass.struts2.action;
- import myclass.service.LoginService;
- public class LoginAction {
- private LoginService loginService;
- private String username;
- private String userpassword;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getUserpassword() {
- return userpassword;
- }
- public LoginService getLoginService() {
- return loginService;
- }
- public void setLoginService(LoginService loginService) {
- this.loginService = loginService;
- }
- public void setUserpassword(String userpassword) {
- this.userpassword = userpassword;
- }
- public String execute(){
- System.out.println("我进来了呢");
- System.out.println(this.getUsername());
- System.out.println(this.getUserpassword());
- //调用业务层方法
- loginService.check(username, userpassword);
- return "succ";
- }
- }
接着配置web.xml,struts.xml .applicationcontext.xml文件
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <!-- 加载spring配置文件 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <!-- 加载监听器 -->
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <!--struts2 -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
struts.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.action.extension" value="lp" />
- <constant name="struts.objectFactory" value="spring"></constant>
- <package name="login" namespace="/" extends="struts-default">
- <action name="login" class="login">
- <result name="succ">/succ.jsp</result>
- <result name="fail">/failure.jsp</result>
- </action>
- </package>
- </struts>
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:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <!-- 启动注解
- <context:annotation-config></context:annotation-config>
- -->
- <bean id="loginServiceImp" class="myclass.service.impl.LoginServiceImp">
- </bean>
- <bean id="login" class="myclass.struts2.action.LoginAction">
- <property name="loginService" ref="loginServiceImp"></property>
- </bean>
- </beans>
这样就整合完了.值得注意的事applicationcontext.xml中<bean id="login"与struts.xml 中<action name="login" class="login"> class中的名字要一样,才行,不然会有错误.总之比整合struts1要容易些.