spring框架与struts2框架的整合

spring与struts2的整合

1:单独配置struts
一:导入struts包

二:编写Action
public class JboaEmployeeAction extends ActionSupport {
public String login(){
System.out.println(“login…”);
return SUCCESS;
}
}

三:配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?> index.jsp

四:配置web.xml —struts2的核心控制器

<?xml version="1.0" encoding="UTF-8" ?> index.jsp

五:测试struts配置是否成功
在IE测试输入:
http://localhost:8080/spring3_02/jboaEmployeeAction

2:准备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

contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener

三:在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页最上面一段有说明

3:案例:根据登陆用户,判断职位
目的:突出说明OpenSessionInViewFilter

一:编写login.jsp
<%@ page language=“java” pageEncoding=“UTF-8”%>
<%@ taglib prefix=“s” uri="/struts-tags"%>

登录

登录

二:编写Action
public class JboaEmployeeAction extends ActionSupport {
private JboaEmployeeService jboaEmployeeService;

private JboaEmployee employee;

public String login(){
	System.out.println("login..............");
	
	JboaEmployee newEmployee =jboaEmployeeService.login(employee);
	
	//将登陆的信息都放入session
	Map<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
编写测试代码:
职位是: n e w E m p l o y e e . j b o a P o s i t i o n . p o s N a m e C n < / b r > 欢 迎 : {newEmployee.jboaPosition.posNameCn }</br> 欢迎: newEmployee.jboaPosition.posNameCn</br>{newEmployee.empName }
------------------分割线-------------------------
运行index.jsp之后,获取职位信息【报错】!

解决方案一:
加上lazy="false"立即加载!

解决方案二:(推荐)
关键点:
1:在web.xml配置如下代码,必须放在struts核心控制器的前面
2:访问路径必须带结尾是*.action,否则无法触发.OpenSessionInViewFilter

OpenSessionInViewFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter OpenSessionInViewFilter *.action

4:HibernateCallback实现自定义的分页
一:接口:
/**
* 利用回调函数,实现分页
* @param page
* @param size
* @return
*/
public List find(final int page,final int size);

二:实现:
public List find(final int page, final int size) {
List 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层
–>



二:修改struts.xml

index.jsp

注意【黄色】部分必须一致,但是的id值,不要和action name=“jboaEmployeeAction”>的值相同。

6:Spring中Bean的作用域
目的:需要了解概念,具体课堂不深入探讨这部分内容,感兴趣的同学需要到网络自学。

一:编写applicationContext-test.xml

<bean id="singleton" class="java.util.Date" scope="singleton"></bean>
 <!-- 原型日期   每次新建对象 -->
<bean id="prototype" class="java.util.Date" scope="prototype"></bean>

二:测试作用域的区别
/**

  • @包名 com.jbit.jboa.action

  • @文件名 TestDate.java

  • @作者 Cui.Ge

  • @创建日期 2013-6-5
    */
    public class TestDate {

    public static void main(String[] args) {
    ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext-test.xml”);

     DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
     
     Date s1 = (Date)ac.getBean("singleton");
     Date p1 = (Date)ac.getBean("prototype");
     
     System.out.println("s1 date 1 = "+df.format(s1));
     System.out.println("p1 date 1 = "+df.format(p1));
     
     System.out.println("等待5秒..........................");
     try{
     	Thread.sleep(5000);   //延迟5秒
     }catch(Exception e){
     	e.printStackTrace();
     }
    
     
     Date s2 = (Date)ac.getBean("singleton");
     Date p2 = (Date)ac.getBean("prototype");
     
     System.out.println("s2 date 2 = "+df.format(s2));
     System.out.println("p2 date 2 = "+df.format(p2));
     System.out.println("等待2秒..........................");
     try{
     	Thread.sleep(2000);   //延迟2秒
     }catch(Exception e){
     	e.printStackTrace();
     }
     
     
     Date s3 = (Date)ac.getBean("singleton");
     Date p3 = (Date)ac.getBean("prototype");
     
     System.out.println("s3 date 3 = "+df.format(s3));
     System.out.println("p3 date 3 = "+df.format(p3));
    

    }
    }

猜你喜欢

转载自blog.csdn.net/weixin_43474701/article/details/123421924