spring2.5整合struts2.1.8

在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页面:

Code:
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <html>  
  3.   <head>  
  4.   <title> 登录页面 </title>  
  5.   <link rel=stylesheet href="css/login.css" type="text/css">  
  6.   </head>  
  7.   <body>  
  8.   <h1 align="center">用户登录页面</h1>  
  9.   <hr>  
  10.    <div align="center">  
  11.    <form action="login.lp" method="post">  
  12.    <table  cellspacing=5 border=5 bodercolor=#ffaa00 >  
  13.    <tr><th colspan="3" align="center"  bgcolor=#ffaa00>用户登录</th></tr>  
  14.    <tr>  
  15.    <th rowspan="3" background="images/2.jpg" style="width=90px"></th>  
  16.    <td>用户名:</td><td><input type="text" class="message" name="username"></td></tr>  
  17.    <tr><td>密  码:</td><td><input class="message" type="password" name="userpassword"></td></tr>  
  18.     <tr><td colspan="2" align="center"><input type="submit" value="登录">  <input type="reset" value="重置"></td></tr>  
  19.    </table>  
  20.    </form>  
  21.    </div>  
  22.   </body>  
  23. </html>  

成功跳转页面和失败跳转页面:

Code:
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <html>  
  3.   <head>  
  4.     <title>Succsess</title>  
  5.   </head>  
  6.     <!--默认把值放在了request作用域里面,如果要用session,则需要另外的方法  -->  
  7.   <body>  
  8.   <p> Succsess.${username } ${userpassword }</p><br>  
  9.     <a href="exit.jsp">退出系统</a>  
  10.   </body>  
  11. </html>  
  12.   
  13. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  14. <html>  
  15.   <head>  
  16.     <title>Failure</title>  
  17.   </head>  
  18.     <!--默认把值放在了request作用域里面,如果要用session,则需要另外的方法  -->  
  19.   <body>  
  20.   <p> Failure.${username } ${userpassword }</p><br>  
  21.     <a href="exit.jsp">退出系统</a>  
  22.   </body>  
  23. </html>  

接着写业务类和action类

Code:
  1.   
  2. public interface LoginService {   
  3. public void check(String username,String userpassword);   
  4. }   
Code:
  1. package myclass.service.impl;   
  2.   
  3. import myclass.service.LoginService;   
  4.   
  5. public class LoginServiceImp implements LoginService{   
  6.   
  7.     public void check(String username, String userpassword) {   
  8.         System.out.println("欢迎你来到业务层:"+username+"---"+userpassword);   
  9.            
  10.     }   
  11.   
  12. }   
Code:
  1. package myclass.struts2.action;   
  2.   
  3. import myclass.service.LoginService;   
  4.   
  5. public class LoginAction {   
  6.     private LoginService loginService;   
  7.     private String username;   
  8.     private String userpassword;   
  9. public String getUsername() {   
  10.         return username;   
  11.     }   
  12.     public void setUsername(String username) {   
  13.         this.username = username;   
  14.     }   
  15.   
  16.     public String getUserpassword() {   
  17.         return userpassword;   
  18.     }   
  19.   
  20.     public LoginService getLoginService() {   
  21.         return loginService;   
  22.     }   
  23.     public void setLoginService(LoginService loginService) {   
  24.         this.loginService = loginService;   
  25.     }   
  26.     public void setUserpassword(String userpassword) {   
  27.         this.userpassword = userpassword;   
  28.     }   
  29.   
  30. public String execute(){   
  31.     System.out.println("我进来了呢");   
  32.     System.out.println(this.getUsername());   
  33.     System.out.println(this.getUserpassword());   
  34. //调用业务层方法   
  35.     loginService.check(username, userpassword);   
  36.     return "succ";   
  37. }   
  38. }   

接着配置web.xml,struts.xml .applicationcontext.xml文件

web.xml

Code:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.         <!-- 加载spring配置文件 -->  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>/WEB-INF/applicationContext.xml</param-value>  
  11.     </context-param>  
  12.     <!-- 加载监听器 -->  
  13.     <listener>  
  14.         <listener-class>  
  15.             org.springframework.web.context.ContextLoaderListener   
  16.         </listener-class>  
  17.     </listener>  
  18.     <!--struts2 -->  
  19.        <filter>  
  20.         <filter-name>struts2</filter-name>  
  21.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  22.     </filter>  
  23.   
  24.     <filter-mapping>  
  25.         <filter-name>struts2</filter-name>  
  26.         <url-pattern>/*</url-pattern>  
  27.     </filter-mapping>  
  28.   <welcome-file-list>  
  29.     <welcome-file>index.jsp</welcome-file>  
  30.   </welcome-file-list>  
  31. </web-app>  

struts.xml

Code:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.  <!DOCTYPE struts PUBLIC    
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.     <struts>  
  6.     <constant name="struts.action.extension" value="lp" />  
  7.     <constant name="struts.objectFactory" value="spring"></constant>  
  8.     <package name="login" namespace="/" extends="struts-default">  
  9.     <action name="login" class="login">    
  10.     <result name="succ">/succ.jsp</result>  
  11.     <result name="fail">/failure.jsp</result>  
  12.     </action>  
  13.     </package>  
  14.     </struts>  

applicationcontext.xml

Code:
  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"  
  4.      xmlns:context="http://www.springframework.org/schema/context"  
  5.       xmlns:aop="http://www.springframework.org/schema/aop"  
  6.       xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
  9.     http://www.springframework.org/schema/aop    
  10.     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  11.     http://www.springframework.org/schema/tx   
  12.     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  13.     http://www.springframework.org/schema/context   
  14.     http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  15.     <!-- 启动注解    
  16.    <context:annotation-config></context:annotation-config>  
  17. -->  
  18.    <bean id="loginServiceImp" class="myclass.service.impl.LoginServiceImp">  
  19.    </bean>  
  20.     <bean id="login" class="myclass.struts2.action.LoginAction">  
  21.     <property name="loginService" ref="loginServiceImp"></property>  
  22.     </bean>  
  23. </beans>  

 这样就整合完了.值得注意的事applicationcontext.xml中<bean id="login"与struts.xml 中<action name="login" class="login"> class中的名字要一样,才行,不然会有错误.总之比整合struts1要容易些.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值