现在的位置: 首页 > 综合 > 正文

Spring视频学习(十四)Spring提供的CharacterEncoding和OpenSessionInView功能

2012年10月24日 ⁄ 综合 ⁄ 共 6250字 ⁄ 字号 评论关闭

一、解决乱码

1.新建一个添加的ActionForm:

import org.apache.struts.action.ActionForm;
public class PersonForm extends ActionForm {

	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}

2.编写添加的页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</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>
    <html:form action="/person/manage" method="post">
    	名称:<html:text property="name"/> 
    	<input type="submit" value="提交">
    	<input type="hidden" name="method" value="add">
    </html:form>
    
    <br>
  </body>
</html>

3.编写添加的Action并交给Spring容器管理:

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.persia.model.Person;
import com.persia.service.IPersonService;
import com.persia.struts.formbean.PersonForm;

public class PersonManageAction extends DispatchAction {

	@Resource IPersonService ps;
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PersonForm f=(PersonForm)form;
		ps.save(new Person(f.getName()));
		request.setAttribute("message", "添加成功");
		
		return mapping.findForward("message");
		
	}
}
applicationContex.xml添加以下该bean的配置,以方便使用注解:
<bean name="/person/manage" class="com.persia.struts.PersonManageAction"></bean>

4.配置struts-config文件:

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

<struts-config>
  <form-beans>
     <form-bean name="personForm" type="com.persia.struts.formbean.PersonForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  
  <action-mappings>
    <action path="/person/list"  validate="false">
       <forward name="list" path="/WEB-INF/page/personlist.jsp"></forward>
    </action>
    <action path="/person/manage" parameter="method" validate="false" scope="request" name="personForm">
       <forward name="message" path="/WEB-INF/page/message.jsp"></forward>
    </action>
  </action-mappings>

<!--定义Spring的请求处理器,来根据action的path属性到Spring容器里面寻找这个bean,若找到了则用这个bean来处理用户的请求-->
<controller>
 <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller> 
<!--配置完Spring的请求处理器后,去掉action的type属性和值,因为已经交给Spring容器来处理(可选)当Spring处理器找不到该bean时,才会使用Struts的action-->


  <message-resources parameter="com.persia.struts.ApplicationResources" />
</struts-config>

5.新建message.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <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>
    ${message} <br>
  </body>
</html>

6.配置web.xml来避免中文乱码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 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>
<!-- 解决延迟加载例外问题 -->
	
	
<!--下面是集成Struts的配置-->
  <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>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
<!--以上是集成struts的配置,然后在web-inf下配置struts-config.xml-->  
  
</web-app>

二、解决延迟例外

上面的OpenSessionInViewFilter就是解决Hiberante因session关闭导致的延迟例外问题:

<!-- 使用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>
<!-- 解决延迟加载例外问题 -->

抱歉!评论已关闭.