当前位置:文档之家› SSH集成spring-Struts2整合详细步骤

SSH集成spring-Struts2整合详细步骤

搭建SSH框架spring与struts2的集成1:单独配置struts一:导入struts包二:编写Actionpublic class JboaEmployeeAction extends ActionSupport { public String login(){System.out.println("login..............");return SUCCESS;}}三:配置struts.xml<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""/dtds/struts-2.3.dtd"><struts><package name="sys"namespace="/"extends="struts-default"><action name="jboaEmployeeAction"class="action.JboaEmployeeAction" method="login"><result name="success">index.jsp</result></action></package></struts>四:配置web.xml ---struts2的核心控制器<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""/dtds/struts-2.3.dtd"><struts><package name="sys"namespace="/"extends="struts-default"><action name="jboaEmployeeAction"class="action.JboaEmployeeAction" method="login"><result name="success">index.jsp</result></action></package></struts>五:测试struts配置是否成功在IE测试输入:http://localhost:8080/spring3_02/jboaEmployeeAction2:准备spring与strus2的集成一:导入包struts2-spring-plugin-2.3.14.jar二:配置web.xml---spring的监听器如果导入了struts2-spring-plugin-2.3.14.jar后,没有配置如下代码,则会报错com/opensymphony/xwork2/spring/SpringObjectFactory.java<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>三:在Action中,如何访问业务Bean第一种:按名字匹配(推荐)// jboaEmployeeService这个名字,应该对应业务层bean的id值private JboaEmployeeService jboaEmployeeService;public void setJboaEmployeeService(JboaEmployeeService jboaEmployeeService) { System.out.println("已经注入业务对象jboaEmployeeService.....");this.jboaEmployeeService = jboaEmployeeService;}第二种:按类型匹配关键:根据set方法中的参数类型匹配!在struts.xml中配置常量: P74页最下面一段,和p75页最上面一段有说明<constant name="struts.objectFactroy.spring.autoWire" value="type"></constant> 3:案例:根据登陆用户,判断职位目的:突出说明OpenSessionInViewFilter一:编写login.jsp<%@page language="java"pageEncoding="UTF-8"%><%@taglib prefix="s"uri="/struts-tags"%><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>登录</title></head><body><h1>登录</h1><s:form action="jboaEmployeeAction"method="post"><s:textfield name="employee.empName"label="用户名"/><s:password name="employee.empPwd"label="密码"/><s:submit value="登录"></s:submit></s:form></body></html>二:编写Actionpublic class JboaEmployeeAction extends ActionSupport {private JboaEmployeeService jboaEmployeeService;private JboaEmployee employee;public String login(){System.out.println("login..............");JboaEmployee newEmployee =jboaEmployeeService.login(employee);//将登陆的信息都放入sessionMap<String, Object> session = ActionContext.getContext().getSession();session.put("newEmployee", newEmployee);System.out.println("职位是:"+newEmployee.getJboaPosition().getPosNameCn());String ret = SUCCESS;if (newEmployee == null) {System.out.println("返回登陆................");ret = INPUT;}return ret;}public void setJboaEmployeeService(JboaEmployeeService jboaEmployeeService) {System.out.println("已经注入业务对象jboaEmployeeService.....");this.jboaEmployeeService = jboaEmployeeService;}public JboaEmployee getEmployee() {return employee;}public void setEmployee(JboaEmployee employee) {this.employee = employee;}}三:编写index.jsp编写测试代码:职位是:${newEmployee.jboaPosition.posNameCn }</br>欢迎:${newEmployee.empName }------------------分割线------------------------- 运行index.jsp之后,获取职位信息【报错】!解决方案一:<many-to-one name="jboaPosition" class="entity.JboaPosition" fetch="select" lazy="false"> 加上lazy="false"立即加载!解决方案二:(推荐)关键点:1:在web.xml配置如下代码,必须放在struts核心控制器的前面2:访问路径必须带结尾是*.action,否则无法触发.OpenSessionInViewFilter<filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class></filter><filter-mapping><filter-name>OpenSessionInViewFilter</filter-name><url-pattern>*.action</url-pattern></filter-mapping>4:HibernateCallback实现自定义的分页一:接口:/*** 利用回调函数,实现分页* @param page* @param size* @return*/public List<JboaEmployee> find(final int page,final int size);二:实现:public List<JboaEmployee> find(final int page, final int size) { List<JboaEmployee> result = getHibernateTemplate().executeFind(new HibernateCallback() {public Object doInHibernate(Session session)throws HibernateException, SQLException {Query query = session.createQuery("from JboaEmployee");query.setFirstResult((page-1)*size);query.setMaxResults(size);return query.list();}});return result;}5:struts2与spring整合的第二种方式一:配置applicationContext.xml<!--action层--><bean id="employeeAction"class="action.JboaEmployeeAction"scope="prototype"><property name="jboaEmployeeService"ref="jboaEmployeeService"></property></bean>二:修改struts.xml<action name="jboaEmployeeAction"class="employeeAction"method="login"> <result name="success">index.jsp</result></action>注意【黄色】部分必须一致,但是<bean id="employeeAction">的id值,不要和action name="jboaEmployeeAction">的值相同。

相关主题