Spring MVC uses demo to learn

Source: Internet
Author: User
Tags naming convention

1: First of all generally know the operating principle of the SPRINGMVC framework (image from the network)

2:springmvc in accordance with the Dispatcherservlet.

Here can be about Structs2 contrast, structs2 is in accordance with the filter to expand; The following is a demo to build a springmvc, frame;

3: The construction process is as follows:

A: First, the required jar package is imported: Lib:

  

B: Next configure the SPRINGMVC core Dispatcherservlet

Configure under Project Web. XML:

(Note here: servlet-name [Spmvcdemo] This name, in the following configuration controller XML file need to use! )

C: Next to create the controller XML file;

To create an XML file under Project WebContent;(Important: The naming convention for the file: [Servlet-name]-servlet.xml] For example, here the project control XML configuration named:spmvcdemo-servlet.xml

D: Next in the Created Control XML file (the source is provided at the end)

 

Configure Handlermapping to specify Contorller according to Benaname
Create Bean name=spmvcdemo.do map to hellocontroller (Processing request Class)

D (1): Create a JSP page to make the request I used the demo.jsp simple <form> form to submit the data request:

                        

D (2): note here that action = "" During demo debugging, the request is distributed to the Controller class by action to process the request, and also by the class to do the response;

E: Next Create Controller Control class: (The Controller for this demo is Hellocontroller)

Create requirement: Inherit abstractcontroller override handlerequestinternal method (auto override when created)

E (1): In the Handlerequestinternal method: Modelandview Do response and data transfer:

                          

F: Next, create the demo_to.jsp used to test the data (EL expression)

G: Finally configure Modelandview prefix suffix configuration attempt resolver!! (in the control XML)

is essentially in the JSP page for access stitching url,/demopath/is a folder in the project, easy to manage the project;

H: Completion of the framework of the construction and commissioning project it;

Attached source code:

Spmvcdemo-servlet.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns: context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "http://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.0.xsd "><!--configuration handlermapping directly introduces a class-- <bean class= "Org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" ></bean> <!--Configure Hanlder distribution Contorller--><bean name= "/spmvcdemo.do" class= "Demo_controller". Hellocontroller "></bean><!--configure modelandview prefix suffix--><!--Configure view resolver--><bean class=" Org.springframework.web.servlet.vIew. Internalresourceviewresolver "><!--prefix--><property name=" prefix "value="/demopath/"></property ><!--suffix--><property name= "suffix" value= ". JSP" ></property></bean></beans>

Xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 ">  <display-name>springmvc_ demo</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <servlet>    <servlet-name>SpMvcDemo</servlet-name>    <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class >  </servlet>  <servlet-mapping>    <servlet-name>spmvcdemo</ Servlet-name>    <url-pattern>*.  do </url-pattern>  </servlet-mapping></web-app>
View Code

Hellocontroller.java

 PackageDemo_controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController;Importsun.misc.Request; Public classHellocontrollerextendsAbstractcontroller {@OverrideprotectedModelandview handlerequestinternal (httpservletrequest arg0, httpservletresponse arg1)throwsException {//TODO auto-generated Method StubString Hello= Arg0.getparameter ("Hello"); System.out.println ("----:"+hello); Modelandview Mav=NewModelandview ("Demo_to"); Mav.addobject ("Helloword", "SPRINGMVC" +hello); returnMav; }}

Do not like to touch, technology is limited, thank you for your comments;

Spring MVC uses demo to learn

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.