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来达到这一目的.维持访问是一种艺术形式 ...
随机推荐
- iperf 2.05版本升级到2.0.9
将openwrt trunk 分支上iperf 2.0.9移植到 bb版本上时,编译遇到如下问题: make[6]: Entering directory '/home/hbg/bb/build_d ...
- Centos 7 安装Docker-ce记录
以前尝试过在centos 6上安装Docker , 需要升级内核,支持aufs,比较麻烦:在使用过程中出现过Docker挂掉的情况,官方建议在64 位 centos 7 上运行,本文将安装步骤记录下来 ...
- c++内存对齐原理
转载自http://blog.csdn.net/it_yuan/article/details/24651347 #类中的元素 0. 成员变量 1. 成员函数 2. 静态成员变量 3. 静 ...
- SQL Server代码段
1.cast和convert ' as int) -- 123 ') -- 123 select CAST(123.4 as int) -- 123 select CONVERT(int, 123.4 ...
- Java+selenium chrome 常见的问题WebDriverException: unknown error: call function result missing 'value'
运行chrome浏览器 报错:"main" org.openqa.selenium.WebDriverException: unknown error: call function ...
- String类型作为方法的形参
代码: public class TestString { String str = new String("good"); char [] ch = {'a','b','c'}; ...
- centos6.5 python命令行模式左右建无法使用
我的虚拟机是centos6.5,自带python2.6:安装了Python2.7(安装了pip管理工具)后,在python2.7命令行模式下,左右键及退格键无法使用,基于以上情况,我进行了百度: 第一 ...
- Mybatis调用PostgreSQL存储过程实现数组入参传递
注:本文来源于 < Mybatis调用PostgreSQL存储过程实现数组入参传递 > 前言 项目中用到了Mybatis调用PostgreSQL存储过程(自定义函数)相关操作,由于Pos ...
- Confluence 6 警告的类型
有下面的一些类型的警告. 警告和知识库(Alert and KB) 级别(Level) 默认阈值(Default threshold) 可配置(Configurable) Low free disk ...
- Confluence 6 如何备份存储文件和页面信息
备份的 ZIP 文件包含有 entities.xml,这个 XML 文件包含有 Confluence 的所有页面内容和存储附件的目录. 备份 Zip 文件结构 页面的附件是存储在附件存储目录中的,通过 ...