Spring MVC基础知识整理➣国际化和异常处理
概述
Spring框架为WEB项目提供了国际化以及异常处理机制。所谓的国际化也就是不同国籍,显示不同国籍的语言与符号。异常处理,也就是能够捕获WEB项目下的所有异常信息,并能处理记录这些异常信息机制。
国际化
Spring对国际化的语言采用配置的方式存储到配置文件中,在springservletconfig.xml文件,添加下面语句:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的文件名 -->
<property name="basename" value="messages" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
Spring MVC国际化的方式,可以基于Session,也可以基于Cookie,这里主要基于Session完成国际化,在springservletconfig.xml添加下面拦截器配置
<mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- 基于session国际化-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<!-- 基于Cookie国际化
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />-->
在当前目录下添加如下文件,messages.properties、messages_zh_CN.properties、messages_en_US.properties三个文件,其中messages.properties、messages_zh_CN.properties;
messages_en_US.properties:
umoney=money
udate=date
messages_zh_CN.properties:
umoney=余额
udate=日期
Controller代码如下:
@RequestMapping("/cuser")
public String ShowUser(HttpServletRequest request, HttpServletResponse response,Model model,
@RequestParam(value="langType", defaultValue="zh") String langType)
{
UserInfo Usermodel=new UserInfo();
SessionLang(request, langType);
//CookieLang(request,response,langType);
RequestContext Rq=new RequestContext(request);
model.addAttribute("udate",Rq.getMessage("udate"));
model.addAttribute("umoney",Rq.getMessage("umoney"));
return "Fuser/cuser";
}
/**
* @Title: SessionLang
* @Description: 基于session的国际化
* @param @param request
* @param @param langType 设定文件
* @return void 返回类型
* @throws
*/
public void SessionLang(HttpServletRequest request,String langType)
{
if(langType.equals("zh")){
Locale locale = new Locale("zh", "CN");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
else if(langType.equals("en")){
Locale locale = new Locale("en", "US");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
else
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
}
/**
* @Title: CookieLang
* @Description: 基于Cookie的国际化
* @param @param request
* @param @param response
* @param @param langType 设定文件
* @return void 返回类型
* @throws
*/
public void CookieLang(HttpServletRequest request,HttpServletResponse response,String langType)
{
Locale locale =null;
if(langType.equals("zh")){
locale = new Locale("zh", "CN");
}
else if(langType.equals("en")){
locale = new Locale("en", "US");
}
else {
locale=LocaleContextHolder.getLocale();
}
(new CookieLocaleResolver()).setLocale (request, response, locale);
}
前台JSP页面
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="cuser?langType=zh">中文</a> | <a href="cuser?langType=en">英文</a><br/> 下面展示的是后台获取的国际化信息:<br/>
${umoney}<br/>
${udate}<br/>
</body>
</html>
异常处理
MVC Spring异常处理的方式有2种,一种是写个Class继承HandlerExceptionResolver,在配置文件中,添加Bean配置,另外一种方式,可以通过定义基类控制器,编写异常处理的公用方法,子类继承基类Controller即可。代码配置如下
编写基类Controller:
package justin.com.controllers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
public abstract class BaseController {
@ExceptionHandler
public String exception(HttpServletRequest request,Exception ex)
{
request.setAttribute("exceptionMessage", ex.getMessage());
return "error";
}
}
子类继承BaseController
package justin.com.controllers;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.connector.Request;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/helloworld")
public class HelloWorldController extends BaseController { @RequestMapping(value={"/*","/say"},method=RequestMethod.GET)
public ModelAndView China() throws SQLException
{ ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "Hello World!");
modelAndView.setViewName("HelloWorld/CIndex");
int Result=/;
return modelAndView;
}
}
当打开该页面,页面自动跳转到错误Controller中,完成数据的异常捕获与显示。
Spring MVC基础知识整理➣国际化和异常处理的更多相关文章
- Spring MVC基础知识整理➣拦截器和自定义注解
概述 Spring MVC中通过注解来对方法或者类进行动态的说明或者标注,类似于配置标识文件的属性信息.当标注的类或者方式被使用时候,通过提取注解信息来达到对类的动态处理.在 MVC中,我们常用的注解 ...
- Spring MVC基础知识整理➣环境搭建和Hello World
概述 Spring MVC属于SpringFrameWork的产品,采用Model-View-Controller进行数据交互,已经融合在Spring Web Flow里面.Spring 框架提供了构 ...
- Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库
概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...
- Spring MVC基础知识整理➣数据校验与格式化
概述 将view中Form的数据提交到后台之后,后台如何验证数据的有效性?在这里Spring MVC提供了相应的Hibernate类包(hibernate-validator-4.3.1.Final. ...
- Spring MVC基础知识整理➣View与Controller数据交互
概述 Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点.MVC中,我们将View中的数据传递到Controlle ...
- Spring框架系列(一)--Spring MVC基础知识
Web项目开发过程中一般都是使用MVC(Model-View-Controller)模式,早先的Struts2到Spring MVC,再到现在Spring Boot,都是相似的思 路.Spring B ...
- spring mvc 基础知识
spring mvc 在web.xml中的配置: 例子: <?xml version="1.0" encoding="UTF-8"?> <we ...
- 【OGG】OGG基础知识整理
[OGG]OGG基础知识整理 一.GoldenGate介绍 GoldenGate软件是一种基于日志的结构化数据复制软件.GoldenGate 能够实现大量交易数据的实时捕捉.变换和投递,实现源数据库与 ...
- Kali Linux渗透基础知识整理(四):维持访问
Kali Linux渗透基础知识整理系列文章回顾 维持访问 在获得了目标系统的访问权之后,攻击者需要进一步维持这一访问权限.使用木马程序.后门程序和rootkit来达到这一目的.维持访问是一种艺术形式 ...
随机推荐
- windows系统下简单nodejs安装及环境配置
相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼,这里不想谈太多的nodejs的相关信息.只说一下,windows系统下简单nodejs环境配置 相信 ...
- 【转】Leveldb源码分析——1
先来看看Leveldb的基本框架,几大关键组件,如图1-1所示. Leveldb是一种基于operation log的文件系统,是Log-Structured-Merge Tree的典型实现.LSM源 ...
- hibernate框架学习之Session管理
Session对象的生命周期 lHibernate中数据库连接最终包装成Session对象,使用Session对象可以对数据库进行操作. lSession对象获取方式: •加载所有配置信息得到Conf ...
- struts2框架之拦截器(参考第二天学习笔记)
拦截器 1. 什么是拦截器 1). 与JavaWeb中的Filter比较相似. 2). 拦截器只能拦截Action!!! 2. Struts中定义了很多拦截器,其中defaultStack中的拦截器会 ...
- xshell访问内网虚拟机
1 关闭虚拟机防火墙 chkconfig iptables off 2 查看VMware Network Adapter VMnet8的ip地址 3 虚拟机nat中设置端口转发,抓发至虚拟机内Linu ...
- jmeter设置全局变量
2017年4月20日 10:07:37 星期四 情景, 从第一个请求的结果中匹配出code, 当作参数去做下次请求 以抢红包为例: 1. 创建红包, 并从返回结果中获取红包code 2. 将code设 ...
- springcloud-3:required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.
在写客户端程序的时候,总是报'com.netflix.discovery.DiscoveryClient' that could not be found. 原因在于导入了错误的类:com.netfl ...
- signal & slot
The Qt signals/slots and property system are based on the ability to introspect the objects at runti ...
- lua内存监测和回收
以下来自书籍<Cocos2d-x之Lua核心编程> 1.----------------------------------------- 若想查看程序当前的内存占用情况,可以使用Lua提 ...
- 如何去掉li标签的重叠边框
当我们的li标签设置了border的时候就会出现重叠边框,如何去掉呢,见代码 html代码 <ul class="friendLink_list"> <li> ...