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来达到这一目的.维持访问是一种艺术形式 ...
随机推荐
- 设计模式C++学习笔记之一(Strategy策略模式)
无意中,从网上下到一本电子书<24种设计模式介绍与6大设计原则>,很好奇这里有24种设计模式,印象中GOF写的<设计模式>(Design Patterns),好像只有23种吧. ...
- 转-CSRF——攻击与防御
0x01 什么是CSRF攻击 CSRF是Cross Site Request Forgery的缩写(也缩写为XSRF),直译过来就是跨站请求伪造的意思,也就是在用户会话下对某个CGI做一些GET/PO ...
- Mybatis--01
mybatis 封装jdbc访问代码的一个框架 (hibernate) ORM对象关系映射 SpringMVC:用来封装servlet的框架 (struts) Spring:体系整合框架,其他框架的 ...
- (并发编程)进程池线程池--提交任务2种方式+(异步回调)、协程--yield关键字 greenlet ,gevent模块
一:进程池与线程池(同步,异步+回调函数)先造个池子,然后放任务为什么要用“池”:池子使用来限制并发的任务数目,限制我们的计算机在一个自己可承受的范围内去并发地执行任务池子内什么时候装进程:并发的任务 ...
- wireshark找(检测)不到(捕获)网卡的解决办法
1 前言 有时候打开wireshark,会提示找不到可用网卡,此时是因为NetGroup Packet Filter Driver 服务没有开启. 环境:笔记本 系统:Win10 网络:WIFI 2 ...
- CircleView
源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/CircleViewDemo.zip 自定义View控件实现圆形的背景+居中的文字,主要代码如下: ...
- jQuery页面滚动底部加载数据
$(window).scroll(function () { var scrollTop = $(this).scrollTop(); var scrollHeight = ...
- 在多任务(RTOS)环境中使用看门狗
最近在SEGGER的博客上看到一篇有关在实时操作系统使用看门狗的文章.从一个失败的太空项目出发,分析了看门狗的作用及使用,自我感觉很有启发,特此翻译此文并推荐给各位同仁.为了阅读方便,有些航天领域名词 ...
- Java中关于string的些许问题及解析
问题一:String 和 StringBuffer 的区别JAVA 平台提供了两个类: String 和 StringBuf fer ,它们可以储存和操作字符串,即包含多个字符的字符数据.这个 Str ...
- 基于 Confluence 6 数据中心的 SAML 单点登录设置 SSL/TLS
请确定 SAML 授权是安全和私有的,你需要在你的应用程序中设置SSL/TLS. 请参考in the application. See Running Confluence Over SSL or H ...