The first spring MVC program

Source: Internet
Author: User

Recent company projects to start using spring MVC instead of Struts2, learn about the use of spring MVC. This is the first spring MVC program, using both XML and annotations.

First, build using XML format

1, using SPRINGMVC, preferred to configure the Interceptor and filter in Web. xml

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:web= "Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"ID= "webapp_id"version= "2.5">        <servlet>        <!--Hello this name needs to correspond to the spring configuration file that follows -        <Servlet-name>Hello</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <!--set the start priority level -        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Hello</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping>    <!--filter to set the encoding -    <Filter>        <Filter-name>Characterfilter</Filter-name>        <Filter-class>Org.springframework.web.filter.CharacterEncodingFilter</Filter-class>        <Init-param>            <Param-name>Enconding</Param-name>            <Param-value>Utf-8</Param-value>        </Init-param>    </Filter>    <filter-mapping>        <Filter-name>Characterfilter</Filter-name>        <Url-pattern>/</Url-pattern>    </filter-mapping></Web-app>

The servlet's name, hello, is not random and needs to correspond to the servlet behind it. Filter is the encoding filter.

2. Define the SPRINGMVC configuration file under Web-inf hello-servlet.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd " >    <!--define a/welcome, when the request to/welcome, the interceptor interception, and then to call the corresponding controller, business processing -    <BeanID= "/welcome"class= "Com.springmvc.web.WelcomeController"></Bean>    <!--defines a view that is used to process the returned view prefix and suffix respectively to define the corresponding page of the view prefix path and suffix path view return welcome corresponds to the path is/web-inf/page/welcome.jsp -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/web-inf/page/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean></Beans>

3. Custom controller, inherit from Abstractcontroller

 PackageCom.springmvc.web;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController;//define a controller that needs to inherit Abstractcontroller Public classWelcomecontrollerextendsabstractcontroller{@OverrideprotectedModelandview handlerequestinternal (httpservletrequest arg0, httpservletresponse arg1)throwsException {System.out.println ("========="); return NewModelandview ("Welcome"); }    }

4, finally set up welcome.jsp under/web-inf/page/, then access Http://127.0.0.1:8080/Spring_Hello/welcome in the browser.

Second, the use of annotated way to write the first SPRINGMVC program. (This is the most commonly used method of our daily development)

1. Also configure Servlets and filter in Web. XML, as in

2. Note Scanning package and open annotation mode in Hello-servlet.xml

Add two sentences in front of the original XML interceptor

<!---    <base-package = "Com.springmvc.web"  ></context:component-scan>    <!---     <mvc:annotation-driven/>

3, or to configure Internalresourceviewresolver view control, and add prefix and suffix properties

4, write Hellocontroller control class (this time does not need to register in the Hello-servlet.xml, also do not need to inherit from the spring controller)

 PackageCom.springmvc.web;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classHellocontroller {@RequestMapping ({"/hello", "/"})     PublicString Hello () {return"Hello"; } @RequestMapping ("/welcome.html")     PublicString Welcome () {return"Welcome"; }}

Call Http://127.0.0.1:8080/Spring_Hello/hello to jump to the hello.jsp page (of course, the hello.jsp file needs to be created first)

Call http://127.0.0.1:8080/Spring_Hello/welcome.html, you can jump to welcome.jsp. (Unlike the welcome you just called, the path you just did not have. html suffix)

The first spring MVC program

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.