Simple SpringMVC classic cases and springmvc classic cases

Source: Internet
Author: User

Simple SpringMVC classic cases and springmvc classic cases

Topic: Build a SpringMVC-based HelloWord Web Project

Purpose: Quick experience of SpringMVC

Solution:

1. Create a project named SpringMVC

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> </beans>

 

4. Configure the servlet -- DispatcherServlet front-end controller encapsulated in Spring in web. xml and specify the spring-mvc.xml File

<? 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_2_5.xsd" id = "WebApp_ID" version = "2.5"> <display-name> SpringMVC </display-name> <servlet-name> SpringMVC </ servlet-name> <! -- DispathcherServlet front-end Controller --> <servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class> <init-param> <! -- Variable name --> <param-name> contextConfigLocation </param-name> <! -- Specify the SpringMVC configuration file name --> <param-value> classpath: spring-mvc.xml </param-value> </init-param> <! -- Load-on-startup is equal to 1, the Servlet is instantiated when the container starts --> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <! -- Corresponds to the Servlet name above --> <servlet-name> SpringMVC </servlet-name> <! -- Used to match client requests --> <url-pattern> *. action </url-pattern> </servlet-mapping> </web-app>

 

5. Configure the HandlerMapping component in the spring-mvc.xml ---------------- role ------> set the client request and Controller

[InternalResourceViewResolver component] -- function ------> set view Configuration

[HelloController] ------------------------ function -------> test request processing

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemaLocation = "http://www.springframework.org/schema/beans ht Tp: // www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springfr Amework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <! -- Define the client request ing --> <! -- HeanlerMapping is one of the core components of Spring --> <bean id = "headlerMapping" class = "org. springframework. web. servlet. handler. simpleUrlHandlerMapping "> <property name =" mappings "> <map> <entry key ="/hello. action "> <value> helloController </value> </entry> </map> </property> </bean> <! -- Add the HelloController Bean --> <bean id = "helloController" class = "controller. HelloController"/> <! -- Define the view interpreter (one of the core Spring components) --> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value =" WEB-INF/jsp/"/> <property name =" suffix "value = ". jsp "/> </bean> </beans>

 

6. Compile HelloController. [Note: the Controller Interface must be implemented]

Package controller; 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 implements Controller {@ Override public ModelAndView handleRequest (HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {ModelAndView mv = new ModelAndView ("hello"); System. out. println ("processing hello. action request "); return mv ;}}

7. Add the "jsp" folder under the WEB-INF folder and add hello. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

8. Start running, boys ~ Then access http: // localhost/SpringMVC/hello. action. The effect is as follows:

  

  

 

Related Article

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.