SPRINGMVC application------An introductory example based on annotations
Catalogue
1. Configure front in the Web. xml file
2. Configuring the processor Mapper, processor adapter, and view resolver in the Springmvc.xml file
3, Write Handler
4. Write the View index.jsp
5. Enter in the browser: Http://localhost:8080/SpringMVC-003/hello
The first two blogs we have explained the XML-based getting started instance, and the detailed process of SPRINGMVC running. However, we find that XML-based configuration is still cumbersome, and that there can only be one method for each Handler class, which is certainly not possible to develop in real-world development. So this blog is about the most SPRINGMVC configuration-based annotation configuration in real-world development.
The project structure is:
1. Configure front in the Web. 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_3_0.xsd "id=" webapp_id "version=" 3.0 "><display-name>SpringMVC_01</display-name> <!--Configuring the front-end controller Dispatcherservlet--<servlet> < Servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--Springmvc.xml is the SPRINGMVC global profile that you created, loaded with contextconfiglocation as the parameter name if you do not configure Contextconfiglocation, the default load is/web-inf/servlet name-servlet.xml, here is springmvc-Servlet.xml-<init-param> <param-name>contextConfigLocation</param-name> <param-value>c lasspath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <SERVL Et-name>springmvc</servlet-name> <!--First configuration: *. Do, you can also write *. Action, and so on, indicates that URLs that end with. Do or end with. Action are resolved by the front-end controller Dispatcherservlet The second configuration:/, all access URLs are parsed by Dispatcherservlet, but it is best to configure the static files without Dispatcherservlet to parse the error configuration:/*, note that this can not be configured to do so, should be written, and finally forwarded to the JSP page, will still be resolved by Dispatcherservlet, and this time will not find the corresponding handler, thus error!!! -<url-pattern>/</url-pattern> </servlet-mapping></web-app>
2. Configuring the processor Mapper, processor adapter, and view resolver in the Springmvc.xml file
<?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"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx.xsd "><!--note Processor Mapper--<beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" ></bean> <!-- Note Processor adapter-<beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" ></bean> <!-- Using mvc:annotation-driven can replace the above mapper and adapter this will be loaded by default many parameter binding methods, such as the JSON conversion parser is loaded by default, so the following configuration is preferred-<!--<mvc:annotation-driven></mvc:annotation-driven>-<!--single Configuration handler--&L t;! --<beanclass= "Com.ys.controller.HelloController" ></bean>-<!--bulk configuration handler, specifying the scanned package full name--and <context:co Mponent-scan base- Package= "Com.ys.controller" ></context:component-scan> <!--configuration View Resolver--<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <!--returns the prefix of the view page--<proper Ty name= "prefix" value= "/web-inf/view/" ></property> <!--return page suffix--<property name= "Suffi X "value=". jsp "></property> </bean></beans>
3, Write Handler
PackageCom.ys.controller;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView;
Note the use of @controller annotations and @requestmapping annotations//using @controller annotations indicates that this class is a handler@Controller Public classHellocontroller {//@RequestMapping Note The URL that represents the access in parentheses@RequestMapping ("Hello") PublicModelandview Hello () {Modelandview Modelview=NewModelandview (); //similar to Request.setattribute ()Modelview.addobject ("name", "Zhang San"); //Configure the returned view name, since we have configured prefixes and suffixes in springmvc.xml, it is good to write the view name directly hereModelview.setviewname ("Index"); //modelview.setviewname ("/web-inf/view/index.jsp"); returnModelview; } }
4. Write the View index.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" >Hello:${name} </body>
5. Enter in the browser: Http://localhost:8080/SpringMVC-003/hello
SPRINGMVC application------An introductory example based on annotations