如果要在Spring MVC应用程序中使用多视图解析程序,则可以使用order属性设置优先级顺序.以下示例显示如何在Spring Web MVC框架中使用 ResourceBundleViewResolver 和 InternalResourceViewResolver .
<bean class = "org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name = "basename" value = "views" /> <property name = "order" value = "0" /> </bean> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value = "/WEB-INF/jsp/" /> <property name = "suffix" value = ".jsp" /> <property name = "order" value = "1" /> </bean>
这里,order属性定义了一个视图解析器的排名.在这里,0是第一个解析器,1是下一个解析器,依此类推.
hello.(class) = org.springframework.web.servlet.view.JstlView hello.url = /WEB-INF/jsp/hello.jsp
例如,使用上面的配置,如果URI :
/hello,请求DispatcherServlet将请求转发给views.properties中由bean hello定义的hello.jsp.
首先,让我们一起工作Eclipse IDE到位并考虑以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序.
Step | 描述 |
---|---|
1 | 在Spring MVC - Hello World章节中解释,在com.it1352包下创建一个名为TestWeb的项目. |
2 | 在com.it1352下创建一个Java类HelloController package. |
3 | 创建一个视图文件hello jsp子文件夹下的.jsp. |
4 | 在SRC文件夹下创建属性文件views.properties. |
5 | 下载JSTL库 jstl.jar .把它放在你的CLASSPATH中. |
6 | 最后一步是创建源文件和配置文件的内容并导出应用程序,如下所述. |
package com.it1352; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.ui.ModelMap; @Controller @RequestMapping("/hello") public class HelloController{ @RequestMapping(method = RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "Hello Spring MVC Framework!"); return "hello"; } }
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package = "com.IT屋" /> <bean class = "org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name = "basename" value = "views" /> <property name = "order" value = "0" /> </bean> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value = "/WEB-INF/jsp/" /> <property name = "suffix" value = ".jsp" /> <property name = "order" value = "1" /> </bean> </beans>
hello.(class) = org.springframework.web.servlet.view.JstlView hello.url = /WEB-INF/jsp/hello.jsp
<%@ page contentType = "text/html; charset = UTF-8" %> <html> <head> <title>Hello World</title> </head> <body> <h2>${message}</h2> </body> </html>
完成创建源文件和配置文件后,导出应用程序.右键单击您的应用程序,使用 Export → WAR文件选项并将 HelloWeb.war 文件保存在Tomcat的webapps文件夹中.
现在,启动Tomcat服务器并确保能够使用标准浏览器从webapps文件夹访问其他网页.尝试访问URL : http://localhost:8080/HelloWeb/hello ,如果Spring Web Application一切正常,我们将看到以下屏幕.