spring+struts的集成(第一种集成方案)
spring+struts的集成(第一种集成方案)
原理:在Action中取得BeanFactory对象,然后通过BeanFactory获取业务逻辑对象
1、spring和struts依赖库配置
* 配置struts
--拷贝struts类库和jstl类库
--修改web.xml文件来配置ActionServlet
--提供struts-config.xml文件
--提供国际化资源文件
* 配置spring
--拷贝spring类库
--提供spring配置文件
2、在struts的Action中调用如下代码取得BeanFactory
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
3、通过BeanFactory取得业务对象,调用业务逻辑方法
WEB-INF下
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext-*.xml</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.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.bjsxt.usermgr.forms.LoginActionForm"/>
</form-beans>
<action-mappings>
<action path="/logininput"
forward="/login.jsp"
></action>
<action path="/login"
type="com.bjsxt.usermgr.actions.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
WebContent或WebRoot下
index.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>spring和struts的集成(第一种方案)</title>
</head>
<body>
<h1>spring和struts的集成(第一种方案)</h1>
<hr>
<a href="logininput.do">登录</a>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录</title>
</head>
<body>
<form action="login.do" method="post">
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
${loginForm.username },用户登录成功!
</body>
</html>
src下
applicationContext-beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="userManager" class="com.bjsxt.usermgr.manager.UserManagerImpl"/>
</beans>
MessageResources.properties
# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Blank Application
welcome.heading=Welcome!
welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.)
src.com.bjsxt.usermgr.actions下LoginAction.java
package com.bjsxt.usermgr.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.bjsxt.usermgr.forms.LoginActionForm;
import com.bjsxt.usermgr.manager.UserManager;
import com.bjsxt.usermgr.manager.UserManagerImpl;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm)form;
//UserManager userManager = new UserManagerImpl();
//userManager.login(laf.getUsername(), laf.getPassword());
//BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-beans.xml");
//UserManager userManager = (UserManager)factory.getBean("userManager");
//userManager.login(laf.getUsername(), laf.getPassword());
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
//ApplicationContext pc = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
UserManager userManager = (UserManager)factory.getBean("userManager");
userManager.login(laf.getUsername(), laf.getPassword());
return mapping.findForward("success");
}
}
src.com.bjsxt.usermgr.forms下 LoginActionForm.java
package com.bjsxt.usermgr.forms;
import org.apache.struts.action.ActionForm;
public class LoginActionForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
src.com.bjsxt.usermgr.manager 下
UserManager.java
package com.bjsxt.usermgr.manager;
public interface UserManager {
public void login(String username, String password);
}
UserManagerImpl.java
package com.bjsxt.usermgr.manager;
public class UserManagerImpl implements UserManager {
public void login(String username, String password) {
System.out.println("UserManagerImpl.login() -- username=" + username);
}
}
分享到:
相关推荐
尚学堂的Struts+Hibernate+Spring手把手教程旨在帮助学习者掌握这些框架的使用,从Servlet基础开始,包括Servlet的编写、映射,再到JavaBeans的规范,然后深入理解Struts的MVC模式、配置方法,以及Model1和Model2的...
这个“Struts+Hibernate+Spring”PPT是尚学堂的一份教学资料,旨在帮助开发者掌握这些框架的使用。 Struts是一个开源的Model-View-Controller(MVC)框架,它的目标是简化Web应用的开发过程。MVC模式将应用分为三个...
Struts+Hibernate+Spring是Java Web开发中的一种经典组合,...通过尚学堂的这套手把手教程,你可以系统地学习和实践SSH框架,从理论到实践,逐步提升Java Web开发技能,从而能够构建出灵活、可扩展的多层Web应用程序。
通过尚学堂的"Struts+Hibernate+Spring"课件,学习者可以深入理解这三个框架的基本概念、工作原理以及如何将它们整合在一起。课程旨在使学员熟练掌握这些技术,构建出灵活、可扩展的多层Web应用程序。课程内容涵盖了...
"尚学堂Spring学习笔记" 本文档记录了尚学堂Spring学习笔记的重要知识点,涵盖了Spring配置文件的设置、普通属性的注入、自定义属性编辑器、公共属性的注入、Spring注解等内容。 一、Spring配置文件的设置 在...
1. **实体(Entities)**:在Spring Data JPA中,实体是数据库表的映射。通过在类上添加`@Entity`注解,我们可以声明一个Java类为数据库表的代表。`@Id`注解用于标识主键字段。 2. **Repository**:Repository是...
1. **Spring配置**:首先,我们需要引入Spring的核心jar包,如`spring-context`、`spring-beans`、`spring-web`和`spring-webmvc`。这些jar包包含了Spring的IoC(Inversion of Control,控制反转)和AOP(Aspect ...
struts hibernate spring开发用到的jar包整合 struts hibernate spring三者的API中文文档 还有尚学堂ssh培训的学习笔记 看到网上关于ssh的都是一些零散的,把struts hibernate spring用到的资料整合起来,免得到处...
本项目提供了一套完整的SpringBoot学习视频源码,涵盖Java和HTML编程语言,总计包含220个文件。其中,Java源文件92个,HTML文件36...这些资源基于北京尚学堂的SpringBoot教程,旨在帮助开发者深入理解SpringBoot框架。
《尚学堂_Spring_0100_模拟Spring》 在深入探讨Spring框架之前,我们需要先理解Spring的核心理念。Spring是一个开源的Java平台,它主要为构建企业级应用提供了全面的编程和配置模型。它的核心特性是依赖注入...
在这个教程或博文中,我们可能将深入理解Spring如何管理和装配集合对象,如List、Set、Map等。这在实际开发中非常重要,因为集合经常被用来存储和处理一组相关的对象。 Spring IoC容器是Spring框架的核心部分,它...
尚学堂的Spring课程深入浅出地讲解了Spring的核心概念和技术,包括AOP(面向切面编程)、自动装配(Autowire)、动态代理与静态代理、注解的使用,以及与Hibernate和Struts的集成,旨在帮助学习者掌握全面的Spring...
《2020最新版SpringCloud(H版&alibaba)框架开发教程》是一套全面而深入的SpringCloud学习资源,涵盖了从基础到高级的各种技术点。这套教程旨在帮助开发者掌握SpringCloud的核心概念和实践技巧,尤其针对H版及阿里...
1. Spring框架的核心库,如`spring-core.jar`, `spring-context.jar`, `spring-beans.jar`, `spring-aop.jar`, `spring-jdbc.jar`, `spring-orm.jar`等,这些是Spring的基本组件。 2. Hibernate的相关库,包括`...
【尚学堂】全新2022版WEB前端HTML5.zip是一个专门针对Web前端开发的教程压缩包,尤其聚焦于HTML5技术。HTML5是超文本标记语言(HTML)的最新版本,它在原有的HTML4基础上引入了众多新的特性和改进,以适应现代互联网...
此外,教程可能还会涉及Spring Cloud Data Flow(数据流处理)、Spring Cloud Stream(消息驱动的微服务间通信)以及Spring Cloud Task(一次性任务调度)等内容。 学习“Spring Cloud尚硅谷”教程,开发者不仅能...
最后,建议配合尚学堂的肖斌Hadoop经典视频教程以及云计算极限班的课程进行系统学习,这些资源将提供详细的指导和实例,帮助你逐步深入理解和掌握Hadoop大数据技术。同时,持续实践和参与社区讨论也是提升技能的重要...
【标题】"s2sh整合实现的oa源码"揭示了这是一个基于Struts2、Hibernate和Spring(简称s2sh)三大开源框架整合构建的企业级办公自动化(OA)系统的源代码。Struts2是用于控制应用程序流程的MVC框架,Hibernate是持久...
Struts2 是一款流行的Java开源框架,用于构建企业级Web应用程序。它主要解决了传统Servlet API在处理请求转发和业务逻辑分离...学习并掌握Struts2,对于理解和实践SSH(Struts2、Spring、Hibernate)开源框架至关重要。