Every time you need a spring MVC Web project to test something, you're struggling with a Web project that doesn't have the simplest spring mvc on hand, write one now.
> Release Notes
To introduce some packages first, spring's IOC, MVC package is needless to say. And Jackson's bag, because by default the controller's method is to return JSON, using this framework.
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactid>ja vax.servlet-api</artifactid> <version>3.1.0</version> </dependency> <dependency> ; <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <v ersion>3.2.10.release</version> </dependency> <dependency> <groupid>org.springfram Ework</groupid> <artifactId>spring-webmvc</artifactId> <version>3.2.10.release</v ersion> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactid>jackson-mapper- Asl</artifacTid> <version>1.9.13</version> </dependency></dependencies>
> Build one of the simplest spring MVC projects
Register the Spring distributor in Web. XML, and also explain where the Distributor based configuration file (here is Spring-mvc.xml) is:
<?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>002 Spring Application</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> & Lt;welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> < Welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring mvc</servlet -name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param- Value>classpath:spring-mvc.xml</param-value> <!--<param-value>/web-inf/spring-mvc.xml</pa Ram-value>-</init-param> <load-on-startup>1</load-on-startup> </servlet> ; <servlet-mapping> <servlet-name>spring mvc</servlet-name> <url-pattern>/</url-pat Tern> </servlet-mapping> </web-app>
So how to configure the configuration 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: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-3.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.0.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "><!--scan annotations-<context:component-scan base- Package= "Com.nicchagil"/> <mvc:annotation-driven/> <!--View Resolver--<Beanclass= "Org.springframework.web.servlet.view.ContentNegotiatingViewResolver" > <property name= "viewresolvers" > <list> <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" val Ue= "/jsp/"/> <property name= "suffix" value= ". jsp"/> </bean> </list> </property> </bean></beans>
This file tells spring:
1, the "Com.nicchagil" package to scan the annotated components, such as "@Controller" and so on.
2. Start the default spring MVC annotations, such as "@ResponseBody" and so on.
3. The Returned view (page) is rendered, where the prefix "/jsp" and the suffix ". jsp" are spliced to locate the path of the page.
Write a simple controller:
PackageCom.nicchagil.controller;ImportJava.util.Map;ImportJava.util.TreeMap;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody, @Controller @requestmapping ("/USER") Public classUsercontroller {@RequestMapping ("/query") PublicString query () {// http://localhost: 8080/003springweb/user/querySYSTEM.OUT.PRINTLN ("Query Trgger.")); return"User/userlist"; } @RequestMapping ("/queryforapp") @ResponseBody PublicObject Queryforapp () {// http://localhost: 8080/003springweb/user/queryforappSystem.out.println ("Queryforapp trgger."); Map<string, object> map =NewTreemap<string, object>(); Map.put ("UserName", "Nick Huang."); returnmap; }}
Then write a page:
Where do you put this page? The "User/userlist" is returned according to the query () method, plus the configuration file configuration prefix "/jsp" and the suffix ". jsp", which we know should be placed in "/jsp/user/userlist.jsp".
<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 " pageencoding=" Iso-8859-1 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >User List demo page. </body>
After starting the project, if you are bound to port 8080, then:
1. Visit "Http://localhost:8080/003SpringWeb/user/query" with a browser and you can see the page.
2. Using the browser to access "HTTP://LOCALHOST:8080/003SPRINGWEB/USER/QUERYFORAPP", you can see the following JSON:
{"UserName": "Nick Huang"}
"Spring" builds the simplest spring MVC project