1. Spring MVC Framework Construction--based on annotation method

Source: Internet
Author: User
Tags bind xmlns

This article is based only on the Spring MVC framework and does not integrate other components such as Hibernate. The spring version used is spring-framework-3.2.2.release-dist.

First the need to copy the package to the Lib folder, in order to be simple, the Spring3.2.2 in the Libs file of the package are copied past, but also need to add some other necessary packages to run properly, as shown below


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> &LT;SERVLET-NAME&GT;SPR Ingmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</ servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> & Lt;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 Web-inf to search for files that end with '-servlet.xml ' and then parses them.

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

<?xml version= "1.0" encoding= "UTF-8"?> <!--see below beans this element tag is not, must have label declaration--<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/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring -mvc.xsd > <!--scan all classes in a Web package to complete bean creation and automatic dependency injection-<context:component-scan base-package= "qust.thb.* "/> <!--support spring3.0 new MVC annotations--<mvc:annotation-driven/> <!--start the Spring MVC Annotation feature, complete the mapping of request and annotation Pojo--&
    Gt <bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMEthodhandleradapter "/> <!--viewresolver-<bean class=" 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>
Then, create the Usercontroller class, specifically 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, I wrote the following:

<body>
${message}
</body>
The directory structure of the project is as follows


Enter the address http://localhost:8080/SpringMVC/user/getUser.do and you'll see the results.

Note:

If you want to use @autowired annotations, you must declare the Autowiredannotationbeanpostprocessor Bean in the Spring container beforehand. The traditional declaration method is as follows:

<bean class= "Org.springframework.beans.factory.annotation. Autowiredannotationbeanpostprocessor "/>
If you want to use annotations such as @required, you also have to do similar configuration, because some annotations we often use, such as @autowired, according to the traditional way a configuration is a bit cumbersome and unnecessary, so spring provides us with <context: The simplified configuration of annotation-config/> automatically helps you to complete the declaration. However, after implementing the definition and injection of the bean, it is also necessary to specify that the beans will be injected, and we will typically configure the scan package path option when using annotations, such as the following

<context:component-scan base-package= "qust.thb.*"/>
This configuration item also includes the ability to automatically inject the above processor, so when you use <context:component-scan/>, you can remove <context:annotation-config/>.

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.