go4it

just do it

Spring视频学习(十六)集成Struts2

1.集成Spring和Hiberante

<?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:context="http://www.springframework.org/schema/context"
	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.5.xsd
	                   http://www.springframework.org/schema/context
	                   http://www.springframework.org/schema/context/spring-context-2.5.xsd
	                   http://www.springframework.org/schema/aop
	                   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	                  ">


	<!-- 配置数据源 -->   
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>   
    <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8"/>   
    <property name="username" value="root"/>   
    <property name="password" value=""/>   
     <!-- 连接池启动时的初始值 -->   
     <property name="initialSize" value="1"/>   
     <!-- 连接池的最大值 -->   
     <property name="maxActive" value="500"/>   
     <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->   
     <property name="maxIdle" value="2"/>   
     <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->   
     <property name="minIdle" value="1"/>   
  </bean>  
  
  <!-- 配置hibernate的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource"><ref bean="dataSource" /></property>
	 <property name="mappingResources">
		    <list>
		      <value>com/persia/model/Person.hbm.xml</value>
		    </list>
		 </property>
		 
	    <!-- 1.首先在sessionFactory里面配置以上3条设置 -->
        <!-- 2.然后得在类路径下面添加一个ehcache.xml的缓存配置文件 -->
        <!-- 3.最后在要使用缓存的实体bean的映射文件里面配置缓存设置 -->
             <!--使用二级缓存--> 
             <!-- 不使用查询缓存,因为命中率不是很高 --> 
             <!-- 使用Ehcache缓存产品 -->  
	 <property name="hibernateProperties">
		    <value>
		        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
		        hibernate.hbm2ddl.auto=update
		        hibernate.show_sql=false
		        hibernate.format_sql=false
		        hibernate.cache.use_second_level_cache=true
       	        hibernate.cache.use_query_cache=false
        	    hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
		    </value>
	     </property>
</bean>

<!-- 配置Spring针对hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	  	<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 配置使用注解的方式来使用事务 -->	
<tx:annotation-driven transaction-manager="txManager"/>

<!-- 使用手工配置的注解方式来注入bean -->
<context:annotation-config></context:annotation-config>

<!--定义要注入的业务bean -->
<bean id="personService" class="com.persia.service.impl.PersonServiceImpl"></bean>

<!--注入Struts 2的action -->
<bean id="personList" class="com.persia.struts2.action.PersonListAction"></bean>
</beans>

2.集成Struts2

(1)引入jar包

image

(2)配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<!--1.指定spring的配置文件,默认从web的根目录开始查找,可以通过spring提供的classpath前缀来配置从类路径开始查找 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>	
<!--2.对Spring容器进行实例化 -->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--以上 2个步骤即实现了在web容器中实例化spring容器的配置,实例化后放到servletContext里面(Application级别)-->

<!--以下是由Spring提供的filter来解决Struts乱码问题 -->
<filter>
  <filter-name>encoding</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
	 <param-name>encoding</param-name>
	 <param-value>UTF-8</param-value>
  </init-param>
</filter>

<filter-mapping>
	<filter-name>encoding</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解决乱码 -->	

<!-- 使用Spring解决Hiberante因session关闭导致的延迟例外问题 -->
<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>/*</url-pattern>
</filter-mapping>
<!-- 解决延迟加载例外问题 -->

<!-- 配置Struts2,默认的配置文件为类路径下的struts.xml-->
 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
<!-- 配置Struts2 -->


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

(3)配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!--指定web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法-->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    
    <!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理
        如果用户需要制定多个请求后缀,则多个后缀之间以英文逗号隔开-->
    <constant name="struts.action.extension" value="do"></constant>
    
    <!--设置浏览器是否缓存静态内容,默认值为true,生产环境下使用,开发阶段最好关闭 -->
    <constant name="struts.serve.static.browserCache" value="false"></constant>
    
    <!--当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false,生产环境下使用,开发阶段最好打开 -->
    <constant name="struts.configuration.xml.reload" value="true"></constant>
    
    <!--开发模式下使用,可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="true" />
      
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  
    <!-- 默认的视图主题 -->
    <constant name="struts.ui.theme" value="simple"></constant>
    
    <!--Struts2集成Spring:所有action对象有Spring来负责创建-->
    <constant name="struts.objectFactory" value="spring"></constant>
    
    
   <!-- Add packages here -->
    <package name="person" namespace="/person" extends="struts-default">
        <global-results>
          <result name="message">/WEB-INF/page/message.jsp</result>
        </global-results>
        
        <!--下面的class为在Spring中配置的action的名称 -->
        <action name="action_*" class="personList" method="{1}">
            <result name="list">/WEB-INF/page/personlist.jsp </result>
            <result name="add">/WEB-INF/page/add_person.jsp </result>
            <result name="edit">/WEB-INF/page/edit_person.jsp </result>
        </action>
    </package>

</struts>

3.action和页面的编写:

(1)action如下:

package com.persia.struts2.action;

import java.util.List;

import javax.annotation.Resource;

import com.persia.model.Person;
import com.persia.service.IPersonService;

public class PersonListAction {

	@Resource IPersonService ps;
	
	private String message;
	private List<Person> persons;
	private Person person;
	
	/**
	 * 显示列表
	 * @return
	 */
	public String list(){
		this.persons=ps.getPersons();
		return "list";
	}
	
	/**
	 * 显示添加界面
	 * @return
	 */
	public String addUI(){
		return "add";
	}
	
	/**
	 * 添加操作
	 * @return
	 */
	public String add(){
       ps.save(person);
       this.message="添加成功";
       return "message";
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public List<Person> getPersons() {
		return persons;
	}
	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}
	
	
}
(2)页面如下:
personList.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
       
    <title>PersonList</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <s:iterator value="persons">
     id=<s:property value="id"/>,name==<s:property value="name"/><br/>
     </s:iterator>
  </body>
</html>
add_person.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
       
    <title>添加</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <s:form action="action_add" namespace="/person">
        名称:<s:textfield name="person.name"></s:textfield>
   <input type="submit" value="发送"/>
     </s:form>
  </body>
</html>

message.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    
    <title>result</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <s:property value="message"/>
  </body>
</html>

posted on 2009-07-24 20:17  cxccbv  阅读(474)  评论(0编辑  收藏  举报

导航