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功能的更多相关文章

  1. [转]Java程序员从笨鸟到菜鸟之(八十三)细谈Spring(十二)OpenSessionInView详解及用法

    首先我们来看一下什么是OpenSessionInView?    在hibernate中使用load方法时,并未把数据真正获取时就关闭了session,当我们真正想获取数据时会迫使load加载数据,而 ...

  2. Spring Boot开启Druid数据库监控功能

    Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容的数据库,包括Oracle.MySQL.Derby.PostgreSQL.SQL Server.H2等.D ...

  3. Spring IOC 之ApplicationContext的其他功能

    正如上面章节所介绍的那样, org.springframework.beans.factory 包提供了管理和操作beans的 基本功能. org.springframework.context包增加 ...

  4. Spring提供的用于访问Rest服务的客户端:RestTemplate实践

    什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...

  5. 使用Spring提供的缓存抽象机制整合EHCache为项目提供二级缓存

      Spring自身并没有实现缓存解决方案,但是对缓存管理功能提供了声明式的支持,能够与多种流行的缓存实现进行集成. Spring Cache是作用在方法上的(不能理解为只注解在方法上),其核心思想是 ...

  6. Spring 系列教程之容器的功能

    Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...

  7. Spring提供的iBatis的SqlMap配置

    1.    applicationContext.xml <!-- Spring提供的iBatis的SqlMap配置--> <bean id="sqlMapClient&q ...

  8. Apache和Spring提供的StopWatch执行时间监视器

    相关阅读 [小家java]java5新特性(简述十大新特性) 重要一跃 [小家java]java6新特性(简述十大新特性) 鸡肋升级 [小家java]java7新特性(简述八大新特性) 不温不火 [小 ...

  9. 006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置

    一.接口Condition.Conditional(原理) 主要提供一下方法 boolean matches(ConditionContext context, AnnotatedTypeMetada ...

随机推荐

  1. 【ZJOI 2008】树的统计

    [题目链接] 点击打开链接 [算法] 树链剖分模板题 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 3000 ...

  2. hdu5822 color

    首先考虑假如是树上的做法:考虑dp,f(i)表示对i的子树染色的方案数.用hash可以实现查询两棵子树是否相同.从而根据hash值排序分类,将相同的子树放在一类. (1)f(i)等于每一类的f(p)乘 ...

  3. sublime text 3中修改tab键为缩进4个空格

    1. 菜单栏里点击 Preferences-> Setting-User, 如图 2. 在弹出来的文本里,添加如下两行: { // 注意只有一个大括号,如果之前有属性,如在之前的属性后确保有 , ...

  4. wxPython学习笔记1

    wxpython介绍: wxPython 是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的.功能键全的  GUI 用户界面. wxPython 是作为优 ...

  5. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时26&&27

    课时26 图像分割与注意力模型(上) 语义分割:我们有输入图像和固定的几个图像分类,任务是我们想要输入一个图像,然后我们要标记每个像素所属的标签为固定数据类中的一个 使用卷积神经,网络为每个小区块进行 ...

  6. SimpleDateFormat并发隐患及其解决

    此文已由作者姚太行授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. SimpleDateFormat被大量使用于处理时间格式化过程,由于该类在创建时会指定一个pattern用于 ...

  7. E20180422-hm

    tint n. 浅色; 色彩,色泽; 气息,迹象,痕迹 vt. 染色; 着色于…; 染(发) introduce vt. 介绍; 引进; 提出; 作为…的开头; variation  n. 变化,变动 ...

  8. MongoDb Samus c# Find函数的使用说明

    长活短说, 网上有一些是不对的 比如 Op.GreaterThan(...).LessThan(..) 不能这么用来表示 ( , ) 而应该这么用: var doc = new Document( a ...

  9. bzoj 1982: [Spoj 2021]Moving Pebbles【博弈论】

    必败状态是n为偶数并且数量相同的石子堆可以两两配对,因为这样后手可以模仿先手操作 其他状态一定可以由先手给后手一步拼出一个必败状态(用最大堆补) #include<iostream> #i ...

  10. hdu 3038 How Many Answers Are Wrong【带权并查集】

    带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...