Spring Learning 10-SPRINGMV Core Components 2 and SPRINGMVC project examples

Source: Internet
Author: User

One, SpringMVC core Interface
5. Viewresolver interface--View parsing interface

Inheritance System:


5. View interface--Views interface



two. Spring MVC Project Example

(1) Create a Dynamic Web project and import the Spring jar package ( don't forget to add Springmvc.jar).
(2) configuration Dispatcherservlet
Dispatcherservlet is the core of SPRINGMVC, registering the following servlet registration information in Web. Xml. Be sure to remember: The servlet, like a companion, wants to get married and register first!

<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/springMVC.xml</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Description
The red inscription indicates the file name of the profile, does not use the default profile name, and uses the Springmvc.xml configuration file.
Where <param-value>**.xml</param-value> can use a variety of writing
1, red inscription does not write, use default value:/web-inf/<servlet-name>-servlet.xml
2. <param-value>/WEB-INF/classes/springMVC.xml</param-value>
3. <param-value>classpath*:springMVC-mvc.xml</param-value>
4. Multiple values are separated by commas
5. Load the configuration file in the global definition
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/web-inf/applicationcontext.xml,
classpath*:springmvc-mvc.xml</param-value>
</context-param>

Among them: <context-param> and <servlet> are elements of the same level, not parent-child element relationships
(3) write the controller, do the core configuration file, and configure the URL and controller mapping
Package com.wepull.test;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.servlet.ModelAndView;
Import Org.springframework.web.servlet.mvc.Controller;
public class Hellocontroller Implementscontroller{

Publicmodelandview HandleRequest (HttpServletRequest request,

HttpServletResponse response) throws Exception {

Request.setattribute ("Hello", "Welcome to spring!");

return new Modelandview ("Welcome");

}

}

We know that struts has a core configuration file in XML format, SPRINGMVC of course, a new XML file under Web-inf: Test-servlet.xml. Note that the test here depends on the name of the servlet. When Dispatcherservlet is loaded, it will attempt to load the application context from this file.

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>

<!--The default mapping processor, which does not need to be explicitly declared, but it is very clear after the declaration which mapping processor is used--
<bean id= "Beannameurlmapping"
Class= "Org.springframework.web.servlet.handler . beannameurlhandlermapping">
</bean>

<!--the name attribute here has two responsibilities, defining both the name of the bean and the URL style that needs to be handled by the controller--

<bean name= "/hello.do"
class= "Com.wepull.test.HelloController" >
</bean>

</beans>

It might be strange to set the Name property instead of the id attribute. This is because the URL contains the xmlid attribute illegal characters-especially the slash (/);

(4) Configure a view parser to combine the controller with the JSP.
Add the parser's configuration fragment to the test-servlet.xml above.

<bean id= "Viewresolver"
Class= "Org.springframework.web.servlet.view. internalresourceviewresolver">

<property name= "prefix" value= "/web-inf/jsp/"/>
<property name= "suffix" value= ". jsp"/>
</bean>

Internalresourceviewresolver the name of the view returned by Modelandview with the prefix of the prefix property configuration, plus the suffix of the suffix property configuration at the end. Because the view named welcome is in the Modelandview returned by Hellocontroller, Internalresourceviewresolver looks for the view at/web-inf/jsp/welcome.jsp.
(5) write the JSP file that is presented to the user.
/web-inf/jsp/welcome.jsp

<%@ page contenttype= "Text/html;charset=utf-8"%>
<%@ page iselignored= "false"%>
<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.0transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title>HelloWorld!</title>
<body>
</body>

When you are done, start the server and enter http://locahost:8080/projectName/hello.do on the browser address bar to access it.

In contrast, we found that SPRINGMVC is similar to struts. There are only two mappings, and the SPRINGMVC is relatively flexible. What are the two places?

(1) Mapping of URLs and actions (back-end controllers).

There is a concept of a mapping processor (handlermapping) in Springmvc. It is actually a processor-mapped bean used to assign a controller to a URL. Spring provides three useful implementations of handlermapping:

--beannameurlhandlermapping

Map a controller to a URL based on the name of the controller

--simpleurlhandlermapping

To map a controller to a URL with a collection of properties defined in the context configuration file

--commonspathmaphandlermapping

To map a controller to a URL by using metadata in the controller code

(2) Mapping of Logical view names and view objects.

There is also the concept of a View parser (Viewresolver) in Springmvc. It determines how the logical view name of the Modelandview object resolves to a view bean used to render the result to the user. There are four viewresolver implementations of spring:

--internalresourceviewresolver

Resolves a logical view name into a View object rendered with a template file, such as a JSP and velocity template

--beannameviewresolver

Resolves a logical view name to a view bean in a dispatcherservlet application context

--resourcebundleviewresolver

Resolves a logical view name to a View object in a Resourcebundler

--xmlviewresolver

Parses the view bean from an XML file, which is separated from the Dispatcherservlet application context.

Spring Learning 10-SPRINGMV Core Components 2 and SPRINGMVC project examples

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.