XmlViewResolver
Used to xml
resolve the view name in the view defined in the file bean
. The following example shows how to use the Spring WEB MVC framework XmlViewResolver
.
The xmlviewresolver-servlet.xml configuration is as follows-
<beanClass="Org.springframework.web.servlet.view.XmlViewResolver"><property Name="Location"> <value>/web-inf/views.xml</value > </property></bean>
Xml
The views.xml configuration is as follows-
<beanId="Hello"Class= "Org.springframework.web . Servlet.view.jstlview "> <property name= "Url" value= "/web-inf/jsp/ Hello.jsp "/></bean>
Xml
For example, using the above configuration, if URI
:
- For a
/hello
request, the DispatcherServlet
request is forwarded to the corresponding defined in the view.xml
hello
hello.jsp
.
First, let's use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web Framework :
- Create a Dynamic Web project called xmlviewresolver .
com.yiibai.springmvc
Create a Java class under the package HelloController
.
jsp
Create a view file under sub-folders: hello.jsp
.
- Download Jstl Library Jstl.jar. Put it in the
CLASSPATH
middle.
- The final step is to create the contents of all the source and configuration files and run the application, as described in detail below.
The complete project file directory structure is as follows-
The code for Hellocontroller.java is as follows-
Package com. Yiibai. Springmvc;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")PublicClassHellocontroller{@RequestMapping(Method = Requestmethod. GET) public String Printhello(modelmap model) {model. AddAttribute("message", "Hello Spring MVC framework!" ); return "Hello"; }}< /c2>
Java
The xmlviewresolver-servlet.xml configuration is as follows-
<beansxmlns="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-scanBase-package="Com.yiibai.springmvc"/><beanClass="Org.springframework.web.servlet.view.XmlViewResolver"><propertyName="Location"> <value>/web-inf/views.xml</value> </ Property> </bean></beans>
Xml
The views.xml configuration is as follows-
<beanId="Hello"Class= "Org.springframework.web . Servlet.view.jstlview "> <property name= "Url" value= "/web-inf/jsp/ Hello.jsp "/></bean>
Xml
The code for hello.jsp is as follows-
<%@PageContentType="Text/html; Charset=utf-8"%>>><title>hello World</title>><body> >${message}></ Body>>
Html
After you finish creating the source and configuration files, publish the application to the Tomcat server.
Now start the Tomcat server, when access URL = = Http://localhost:8080/XmlViewResolver/hello, if the Spring Web application does not have a problem, you should see the following results:
Spring MVC XML View Parser