Spring MVC XML View Parser

Source: Internet
Author: User
Tags tomcat server

XmlViewResolverUsed 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 :

    1. Create a Dynamic Web project called xmlviewresolver .
    2. com.yiibai.springmvcCreate a Java class under the package HelloController .
    3. jspCreate a view file under sub-folders: hello.jsp .
    4. Download Jstl Library Jstl.jar. Put it in the CLASSPATH middle.
    5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.