概述

   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基础知识整理➣国际化和异常处理的更多相关文章

  1. Spring MVC基础知识整理➣拦截器和自定义注解

    概述 Spring MVC中通过注解来对方法或者类进行动态的说明或者标注,类似于配置标识文件的属性信息.当标注的类或者方式被使用时候,通过提取注解信息来达到对类的动态处理.在 MVC中,我们常用的注解 ...

  2. Spring MVC基础知识整理➣环境搭建和Hello World

    概述 Spring MVC属于SpringFrameWork的产品,采用Model-View-Controller进行数据交互,已经融合在Spring Web Flow里面.Spring 框架提供了构 ...

  3. Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库

    概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...

  4. Spring MVC基础知识整理➣数据校验与格式化

    概述 将view中Form的数据提交到后台之后,后台如何验证数据的有效性?在这里Spring MVC提供了相应的Hibernate类包(hibernate-validator-4.3.1.Final. ...

  5. Spring MVC基础知识整理➣View与Controller数据交互

    概述 Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点.MVC中,我们将View中的数据传递到Controlle ...

  6. Spring框架系列(一)--Spring MVC基础知识

    Web项目开发过程中一般都是使用MVC(Model-View-Controller)模式,早先的Struts2到Spring MVC,再到现在Spring Boot,都是相似的思 路.Spring B ...

  7. spring mvc 基础知识

    spring mvc 在web.xml中的配置: 例子: <?xml version="1.0" encoding="UTF-8"?> <we ...

  8. 【OGG】OGG基础知识整理

    [OGG]OGG基础知识整理 一.GoldenGate介绍 GoldenGate软件是一种基于日志的结构化数据复制软件.GoldenGate 能够实现大量交易数据的实时捕捉.变换和投递,实现源数据库与 ...

  9. Kali Linux渗透基础知识整理(四):维持访问

    Kali Linux渗透基础知识整理系列文章回顾 维持访问 在获得了目标系统的访问权之后,攻击者需要进一步维持这一访问权限.使用木马程序.后门程序和rootkit来达到这一目的.维持访问是一种艺术形式 ...

随机推荐

  1. 前端清除缓存方法(微信缓存引起的bug)

    bug1:在新版微信中,部门安卓机子(华为)出现window.location.href/window.location.reload....等方法来刷新本页面链接,发现页面没有被刷新,经过排查,发现 ...

  2. python3+selenium入门16-窗口截图

    有时候需要把一些浏览器当前窗口截图下来,比如操作抱错的时候.WebDriver类下.get_screenshot_as_file()方法可窗口截图,需要传入一个截图文件名的路径.window要用\\当 ...

  3. C# List 作为参数传递的值变化

    一.示例演示 namespace TestConsole { class Program { static void Main(string[] args) { Console.WriteLine(& ...

  4. 四重解法---P1047 校门外的树

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,--,L,都种 ...

  5. 1-HTML Attributes

    下表列举了常用的Html属性 Attribute Description alt Specifies an alternative text for an image, when the image ...

  6. 转载:UML学习(一)-----用例图 (silent)

    原文:http://www.cnblogs.com/silent2012/archive/2011/09/07/2169518.html 1.什么是用例图 用例图源于Jacobson的OOSE方法,用 ...

  7. Windows上的程序员神器Cmder

    用过Windows版本Git的都知道Git自带了Git Bash,这个在很大程度上满足了我的需求,随着Git的版本升级越来越好用 安装Cmder Cmder官网,它把conemu,msysgit和cl ...

  8. 高级UI特效—用SVG码造一个精美的中国地图

    前言 来继续学习SVG,要想深入了解还是要多动手进行实战.关于svg基础可以去看一下我的上一篇文章<SVG前戏—让你的View多姿多彩>,今天就用SVG打造一个精美的UI效果. 正文 先上 ...

  9. Modbus库开发笔记之六:Modbus RTU Master开发

    这一节我们来封装最后一种应用(Modbus RTU Master应用),RTU主站的开发与TCP客户端的开发是一致的.同样的我们也不是做具体的应用,而是实现RTU主站的基本功能.我们将RTU主站的功能 ...

  10. Confluence 6 数据库支持的驱动

    数据库 驱动已捆绑? JDBC 驱动 备注 更多信息 PostgreSQL 9.4-1202 JDBC 41 driver download 我们推荐你使用 JDBC 4 的驱动. 如果你希望使用更新 ...