(转)Spring提供的CharacterEncoding和OpenSessionInView功能
http://blog.csdn.net/yerenyuan_pku/article/details/52902282
前面我们以一种更加优雅的方式集成了Spring4.2.5+Hibernate4.3.11+Struts1.3.8,但是在实际开发中我们会碰到两个问题,在此先不言表,只通过例子将其引出来。
我们先在SSH项目的根目录WebRoot下新建一个JSP页面——index.jsp,即项目首页,其内容为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!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=UTF-8">
<title>Insert title here</title>
</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>
</body>
</html>
- 1
紧接着,在src目录下新建一个cn.itcast.web.formbean包,并在该包下新建一个类——PersonForm.java,用于封装用户提交的数据。其代码为:
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;
}
}
- 1
接下来,在cn.itcast.web.action包下新建一个Action——PersonManageAction.java,用于处理添加用户的请求。其代码为:
public class PersonManageAction extends DispatchAction {
@Resource PersonService personService;
public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
PersonForm formbean = (PersonForm) form;
personService.save(new Person(formbean.getName()));
request.setAttribute("message", "添加成功");
return mapping.findForward("message");
}
}
- 1
顺其自然地,我们还要在Struts配置文件配置该PersonManageAction,这样Struts配置文件的内容就变为:
<?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="cn.itcast.web.formbean.PersonForm"></form-bean>
</form-beans>
<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>
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
</struts-config>
- 1
接下来,我们还要在WEB-INF/page目录下新建一个全局消息显示页面——message.jsp,其内容为:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message } <br/>
</body>
</html>
- 1
最后,我们不要忘了将PersonManageAction交给Spring管理,即需要在Spring配置文件中添加如下配置:
<bean name="/person/manage" class="cn.itcast.web.action.PersonManageAction" />
上面一切工作准备好之后,我们就来引出其中一个问题。我们通过浏览器访问url地址:http://localhost:8080/SSH/,可以看到如下结果: 
虽然我们添加用户是成功了,但是当我们查询数据库person表时,发现存进去的用户名是乱码,如下图所示:
很显然,我们遇到的第一个问题就是当在使用Struts来做增删改查的时候,会发现当我们发送请求参数过去时,Struts得到的中文字符会是乱码,那么我们怎么样解决掉Struts的乱码问题呢?我们在Web应用里面可以使用Spring给我们提供的Filter来解决掉这个问题。即在web.xml文件中添加如下配置:
<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>
- 1
这样,使用Spring就解决了Struts1.3.8的乱码问题了。
当我们使用Struts1.3.8、Spring4.2.5、Hibernate4.3.11进行开发时,还有一个问题——我们经常使用Hibernate的延迟属性时,经常遇到这样的异常,说session被关闭了,导致获取延迟属性的时候,出现一个延迟加载异常,我们在开发应用的时候,经常会出现这种错误。这种错误我们在Web应用里面也可以使用Spring给我们提供的Filter来解决这个问题。即在web.xml文件中添加如下配置:
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 1
这样,使用Spring就解决了Hibernate因session关闭导致的延迟加载异常问题。解决这个问题之后,session是在请求到来时打开,在请求结束时关闭,它横跨Servlet和Jsp,所以当Jsp需要用到某个延迟属性时,这时session仍然处于一种打开状态,所以说它不会出现延迟加载异常。这个在实际开发中非常有用,非常方便。
最终,整个web.xml文件的内容就应为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>SSH</display-name>
<!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>struts</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>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
如须查看源码,可点击Spring提供的CharacterEncoding和OpenSessionInView功能进行下载。
(转)Spring提供的CharacterEncoding和OpenSessionInView功能的更多相关文章
- [转]Java程序员从笨鸟到菜鸟之(八十三)细谈Spring(十二)OpenSessionInView详解及用法
首先我们来看一下什么是OpenSessionInView? 在hibernate中使用load方法时,并未把数据真正获取时就关闭了session,当我们真正想获取数据时会迫使load加载数据,而 ...
- Spring Boot开启Druid数据库监控功能
Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容的数据库,包括Oracle.MySQL.Derby.PostgreSQL.SQL Server.H2等.D ...
- Spring IOC 之ApplicationContext的其他功能
正如上面章节所介绍的那样, org.springframework.beans.factory 包提供了管理和操作beans的 基本功能. org.springframework.context包增加 ...
- Spring提供的用于访问Rest服务的客户端:RestTemplate实践
什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...
- 使用Spring提供的缓存抽象机制整合EHCache为项目提供二级缓存
Spring自身并没有实现缓存解决方案,但是对缓存管理功能提供了声明式的支持,能够与多种流行的缓存实现进行集成. Spring Cache是作用在方法上的(不能理解为只注解在方法上),其核心思想是 ...
- Spring 系列教程之容器的功能
Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...
- Spring提供的iBatis的SqlMap配置
1. applicationContext.xml <!-- Spring提供的iBatis的SqlMap配置--> <bean id="sqlMapClient&q ...
- Apache和Spring提供的StopWatch执行时间监视器
相关阅读 [小家java]java5新特性(简述十大新特性) 重要一跃 [小家java]java6新特性(简述十大新特性) 鸡肋升级 [小家java]java7新特性(简述八大新特性) 不温不火 [小 ...
- 006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置
一.接口Condition.Conditional(原理) 主要提供一下方法 boolean matches(ConditionContext context, AnnotatedTypeMetada ...
随机推荐
- Java类成员访问控制权限
类成员访问控制权限 在JAVA中有四种访问控制权限,分别为:private, default, protected, public 1.Private 如果一个成员方法或变量名前使用了private, ...
- [Selenium] WebDriver 操作 HTML5 中的 video
测试播放,停止播放 http://www.videojs.com/ 示例: package com.learningselenium.html5; import static org.junit.As ...
- Superprime Rib
链接 分析:满足题目条件的必然是1,2,3,5,7,9这几个数字的组合,DFS按位进行即可,边组合边判断是否合法. /* PROB:sprime ID:wanghan LANG:C++ */ #inc ...
- AutoIt获取Gridview中可以修改列的值
有一个界面如上图:黑色框框部分是一个整体,也是一个gridview,如果我想把框框中右侧数据获取出来,该如何操作? 我尝试过了很多途径,都无法成功. 今天,我发现,当鼠标焦点在黑色框框左侧的部分的时候 ...
- Android控件之CalendarView 日历对话框
在Android 3.0中新增的日历视图控件可以显示网格状的日历内容,android.widget.CalendarView是从android.widget.FrameLayout中继承. Calen ...
- Intelidea右键新建选项没有Java class选项
Intelidea创建好项目之后,右键新建Java class的时候发现没有改选项,只有以下几个选项 把sec目录设为源码目录,首先打开Project Structure
- bzoj5073
dp 字符串dp不太会啊... 这种序列和子串的匹配一般设两个状态,dp[i][j]表示当前s匹配到i,t匹配到j的...,g[i][j]表示当前s匹配到i,t匹配到j,i,j必须匹配的...,noi ...
- bash 脚本编程七 将命令输出保存到变量中(转载)
转自:http://blog.csdn.net/csfreebird/article/details/7978699 `符号包含的命令执行完后,可以讲其输出结果保存到变量中 #!/bin/bash v ...
- Android Service完全解析,关于服务你所需知道的一切(下) (转载)
转自:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_blog ...
- Apache Thrift 在Windows下的安装与开发
Windows下安装Thrift框架的教程很多.本文的不同之处在于,不借助Cygwin或者MinGW,只用VS2010,和Thrift官网下载的源文件,安装Thrift并使用. 先从官网 下载这两个文 ...