1. Spring MVC framework construction-Based on Annotation

Source: Internet
Author: User

This article is only based on the spring MVC Framework and does not integrate other components such as hibernate. The spring version is the spring-framework-3.2.2.RELEASE-dist.

First, copy the required package to the Lib folder. For simplicity, copy all the packages in the libs file in spring3.2.2, but add other required packages to run normally. For details, see

Then, modify the Web. xml file as follows:

<?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" 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_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>SpringMVC</display-name>    <servlet>  <servlet-name>springMvc</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <load-on-startup>1</load-on-startup>  </servlet>    <servlet-mapping>  <servlet-name>springMvc</servlet-name>  <url-pattern>/</url-pattern>  </servlet-mapping>    <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

By default, spring MVC goes down to the WEB-INF to search for files ending with '-servlet. xml' and then Parse them.

This article creates an XML file named springMvc-servlet.xml as follows

<? XML version = "1.0" encoding = "UTF-8"?> <! -- See the following beans this element label does not exist, must have a declaration of the label --> <beans xmlns = "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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springfr Renew http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <! -- Scan all classes in the Web package to create beans and automatically inject dependencies --> <context: component-scan base-package = "qust. THB. * "/> <! -- Support spring3.0 new MVC annotation --> <MVC: annotation-driven/> <! -- Start the annotation function of spring MVC to map requests and pojo annotations --> <Bean class = "org. springframework. Web. servlet. MVC. annotation. annotationmethodhandleradapter"/> <! -- Viewresolver --> <beanclass = "org. springframework. web. servlet. view. internalresourceviewresolver "> <property name =" viewclass "value =" org. springframework. web. servlet. view. jstlview "/> <property name =" prefix "value ="/WEB-INF/"/> <property name =" suffix "value = ". JSP "/> </bean> </beans>

Create the usercontroller class as follows:

package qust.thb.usermanage.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/user")public class UserController {@RequestMapping(value = "/getUser.do", method = RequestMethod.GET)public ModelAndView getUser() {ModelAndView mv = new ModelAndView();mv.addObject("message", "Hello World!!!");mv.setViewName("/view/hello");return mv;}}

In the body of Hello. jsp, the following content is written:

<body>${message}</body>

The directory structure of this project is as follows:

Enter http: // localhost: 8080/springmvc/user/getuser. Do to view the result.

Note:

If you want to use the @ autowired annotation, you must declare the autowiredannotationbeanpostprocessor bean in the spring container in advance. The traditional statement is as follows:

<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>

Similar configurations are required if you want to use annotations such as @ required. We often use annotations such as @ autowired, one configuration in the traditional way is cumbersome and unnecessary, so Spring provides us with a simplified configuration method of <context: annotation-config/> to help you automatically complete the Declaration. However, after the bean definition and injection method is implemented, you also need to specify which beans will be injected. In this case, we usually configure the scan package path option using annotations, as shown in the following

<context:component-scan base-package="qust.thb.*" />

This configuration item also includes the ability to automatically inject the preceding processor. Therefore, when <context: component-Scan/> is used, <context: annotation-config/> can be removed.

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.