SpringMVC ------JstlView

摘要: Spring为展现层提供的基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一 。Spring3.0后全面超越Struts,成为最优秀的MVC框架 。SpringMVC通过一套MVC注解,让POJO成为处理请求的控制器,而无须事先任何接口。 支持REST风格的URL请求。 采用了松散耦合可插拔组建结构,比其他MVC框架更具扩展性和灵活性。 本文基于Sping4.x.

    若项目中使用JSTL,则SpringMVC会自动把视图由InternalResourceView转为JstlView

    若使用JSTL的fmt标签则需要在SpringMVC的配置文件中配置国际化资源文件。

<bean id="messageSource" class"org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="i18n"></property>
</bean>

-------------------------------------分割线--------------------------------------------------------------------------

    若希望直接响应通过SpringMVC渲染的页面,可以使用mvc:view-controller标签实现

    path是访问succes.jsp页面的映射路径。view-name是视图名字即success.jsp

    页面不需要进行操作,直接访问,不需要进过handler的页面。

<mvc:view-controller path="springmvc/testJstlView" view-name="success"/>

注意使用这个标签,一般都会配合使用

<mvc:annotation-driven></mvc:annotation-driven>

如果不配置,那么如果你有handler也是转发到这个success视图上面,服务器会提示404,找不到该页面。

------------------------------------------分割线-----------------------------------------------------------------------

    先加入jstl库文件。

    编写国际化资源文件 i18n_zh_CN.properties ,i18n.properties ,i18n_en_US.properties 

    这里我们编写 i18n_en_US.properties文件

i18n.username=username
i18n.password=password

在jsp页面导入fmt标签。

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
    //编写jsp代码
    <fmt:message key="i18n.username"></fmt:message>
    <br/>
    <fmt:message key="i18n.password" ></fmt:message>

在spring.xml中配置国际化资源文件

<bean id="messageSource" class"org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="i18n"></property>
</bean>
原文地址:https://www.cnblogs.com/hihtml5/p/6690281.html