Java进阶知识20 Struts2和Spring整合在一起

1、概述       

  1、Spring负责对象创建
  2、Struts2负责用Action处理请求
  3、整合的关键点:让Struts2框架Action对象的创建交给Spring完成。

2、整合实例  

需要用到的 jar包

       Spring的配置文件(aaa-bbb.xml)建议分层,方便维护。

配置web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   
12     <!-- Spring 监听器 -->
13     <context-param>
14         <param-name>contextConfigLocation</param-name>
15         <param-value>/WEB-INF/classes/beans-*.xml</param-value>
16     </context-param>
17     <listener>
18         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
19     </listener>
20 
21     <!-- Struts 过滤器 -->
22     <filter>
23         <filter-name>struts2</filter-name>
24         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
25     </filter>
26     <filter-mapping>
27         <filter-name>struts2</filter-name>
28         <url-pattern>/*</url-pattern>
29     </filter-mapping>
30 </web-app>

DAO层

 1 //接口
 2 public interface IUserDao {
 3     public String getByName();
 4 }
 5 
 6 //实现
 7 public class UserDaoImpl implements IUserDao {
 8 
 9     public String getByName() {
10         return "DSHORE";
11     }
12 }

Service层

 1 //接口
 2 public interface IUserService {
 3     public String getByName();
 4 }
 5 
 6 //实现
 7 public class UserServiceImpl implements IUserService {
 8 
 9     //注入值,如果没有使用spring框架,则此处需要new对象,否则拿不到值。
10     private IUserDao userDao;// = new UserDaoImpl();
11     
12     public String getByName() {
13         return userDao.getByName();
14     }
15 
16     public IUserDao getUserDao() {
17         return userDao;
18     }
19     public void setUserDao(IUserDao userDao) {
20         this.userDao = userDao;
21     }
22 }

Action层

 1 package com.shore.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 import com.shore.service.IUserService;
 5 
 6 /**
 7  * @author DSHORE/2019-10-26
 8  * 
 9  */
10 public class UserAction extends ActionSupport {
11     private static final long serialVersionUID = -8197510116737054459L;
12 
13     //注入值,如果没有使用spring框架,则此处需要new对象,否则拿不到值。
14     private IUserService userService;// = new UserServiceImpl();
15 
16     public String login() {
17         System.out.println("用户名:" + userService.getByName());
18         return SUCCESS;
19     }
20 
21     public IUserService getUserService() {
22         return userService;
23     }
24 
25     public void setUserService(IUserService userService) {
26         this.userService = userService;
27     }
28 }

struts.xml 配置文件      注意:struts.xml 中 action name 和 bean-action.xml 中的 bean id 名称,一定要保持一致!!!

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <!-- true支持动态方法调用 -->
 8     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 9     <constant name="struts.devMode" value="true" /> <!-- true -->
10 
11     <package name="front" namespace="/front" extends="struts-default">
12         <action name="userAction" class="com.shore.action.UserAction">
13             <result name="success">/success.jsp</result>
14         </action>
15     </package>
16 </struts>

beans-dao.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"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"  
 6        xsi:schemaLocation="
 7        http://www.springframework.org/schema/beans
 8        http://www.springframework.org/schema/beans/spring-beans.xsd
 9        http://www.springframework.org/schema/tx
10        http://www.springframework.org/schema/tx/spring-tx.xsd
11        http://www.springframework.org/schema/aop
12        http://www.springframework.org/schema/aop/spring-aop.xsd">
13        
14        <bean id="userDao" class="com.shore.dao.impl.UserDaoImpl"></bean>
15 </beans>

beans-service.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"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"  
 6        xmlns:p="http://www.springframework.org/schema/p" 
 7        xsi:schemaLocation="
 8        http://www.springframework.org/schema/beans
 9        http://www.springframework.org/schema/beans/spring-beans.xsd
10        http://www.springframework.org/schema/tx
11        http://www.springframework.org/schema/tx/spring-tx.xsd
12        http://www.springframework.org/schema/aop
13        http://www.springframework.org/schema/aop/spring-aop.xsd">
14        
15        <bean id="userService" class="com.shore.service.impl.UserServiceImpl" p:userDao-ref="userDao"></bean>
16 </beans>

beans-action.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"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:tx="http://www.springframework.org/schema/tx"  
 6        xmlns:p="http://www.springframework.org/schema/p" 
 7        xsi:schemaLocation="
 8        http://www.springframework.org/schema/beans
 9        http://www.springframework.org/schema/beans/spring-beans.xsd
10        http://www.springframework.org/schema/tx
11        http://www.springframework.org/schema/tx/spring-tx.xsd
12        http://www.springframework.org/schema/aop
13        http://www.springframework.org/schema/aop/spring-aop.xsd">
14        
15        <!-- scope="prototype":多例。默认是单例(不用写即是默认) -->
16        <bean id="userAction" class="com.shore.action.UserAction" scope="prototype" p:userService-ref="userService"></bean>
17 </beans>

index.jsp 页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@taglib uri="/struts-tags" prefix="s"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'index.jsp' starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25     <s:form action="userAction!login" method="post" namespace="/front">
26         <s:textfield name="name" label="用户名"></s:textfield>
27         <s:password name="password" label="密码"></s:password>
28         <s:submit value="登录"></s:submit>
29     </s:form>
30   </body>
31 </html>

success.jsp 页面

 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 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'success.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26        登录成功!
27   </body>
28 </html>

测试结果图:

 

 

 

 

 

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/11745460.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

posted @ 2019-10-26 20:52  DSHORE  阅读(263)  评论(0编辑  收藏  举报