当前位置: 代码迷 >> Web前端 >> Spring的web一部分――JSP和JSTL
  详细解决方案

Spring的web一部分――JSP和JSTL

热度:106   发布时间:2012-07-08 17:43:44.0
Spring的web部分――JSP和JSTL

Spring把view层技术与MVC框架的其他部分区分开来,view层可以使用Velocity、XSLT、JSP等,例如原先使用的JSP技术,现在想要替换成Velocity模版,只需要更改配置文件就行。

下面来说一下JSP和JSTL:

在Spring中使用JSP和JSTL与使用其他的视图技术一样,都需要一个用来解析视图的解析器,常用的是在WebApplicationContexy中定义的InternalResourceViewResolver和ResourceBundleViewResolver

1、使用ResourceBundleViewResolver视图解析器:

首先在要定义视图解析器定义一个bean,

<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basename" value="views"/>
</bean>

其次要定义一个属性文件views.properties

welcome.class=org.springframework.web.servlet.view.JstlView
welcome.url=/WEB-INF/jsp/welcome.jsp

productList.class=org.springframework.web.servlet.view.JstlView
productList.url=/WEB-INF/jsp/productlist.jsp

a、class是类,b、url是映射

?

2、使用InternalResourceViewResolver视图解析器:

只需要在配置文件中定义一个bean:

?
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp">
</bean> 

?推荐用该解析器,并在WEB-INF下的目录下来存放jsp文件,避免被客户端直接访问

?

  相关解决方案