论坛首页 Java企业应用论坛

spring 3 mvc ResourceBundleViewResolver 用法

浏览 3032 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-12-21  

ResourceBundleViewResolver 是用于本地化映射的view解析器,其工作原理是通过properties文件来达到映射的目的

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

	<!-- Enables the Spring MVC @Controller programming model
	<annotation-driven />
-->
	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory
	<resources mapping="/resources/**" location="/resources/" />
	-->

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean id="bundleViewResolver" name="bundleViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
	<!--
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
		 -->
		 <!-- 指定了映射文件的basename, properties文件的命名格式就必须为views_xx_xx.properties 例如views_zh_CN.properties 如果不指定本地化properties文件,默认文件梶 views.properties -->
		 <beans:property name="basename" value="views"></beans:property>
	</beans:bean>
	<beans:bean id="homeController" class="com.mvc.controllers.HomeController">
	<beans:property name="user" ref="user"></beans:property>
	</beans:bean>

	<beans:bean id="simpleUrlMapingHandler" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<beans:property name="mappings">
	<beans:value>
	/home.do=homeController
	/index.do=homeController
	</beans:value>
	</beans:property>
	</beans:bean>

	<beans:bean id="internalPathMethodNameResolver"
          class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
          <!--
        <beans:property name="suffix" value="Handler"/>
         -->
    </beans:bean>
	<!-- Imports user-defined @Controller beans that process client requests
	<beans:import resource="controllers.xml" />
	-->
</beans:beans>

 下面是views.properties的文件,必须在web应用的classpath下。

spring 3的用法为

index.(class)=org.springframework.web.servlet.view.JstlView
index.url=WEB-INF/views/home.jsp
在pro spring 2.5的实例中使用的用法为

index.class=org.springframework.web.servlet.view.JstlView
index.url=WEB-INF/views/home.jsp

 

接着是controller的代码

package com.mvc.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.mvc.entity.User;

//@Controller
public class HomeController extends MultiActionController {

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */

	private User user;
	public User getUser() {
		return user;
	}
//	@Autowired
	public void setUser(User user) {
		this.user = user;
	}
//	@RequestMapping(value="/", method=RequestMethod.GET)
	public ModelAndView home(HttpServletRequest request,
            HttpServletResponse response){
		//System.out.println(user.getName());
		if(user != null){
			System.out.println("true");
			System.out.println(user.getName());
		}
		System.out.println("home");
		ModelAndView mav =  new ModelAndView();
		mav.setViewName("index");
		return mav;
	}

	public ModelAndView index (HttpServletRequest request,
            HttpServletResponse response){
		System.out.println("index");
		ModelAndView mav = new ModelAndView("index","index", null);
		return mav;

	}
}
 

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics