Project structure
Xml
<?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>xnb_web_api</ display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value> xnb_ Web_api.root </param-value> </context-param> <welcome-file-list> <welcome-file>/index.js P</welcome-file> </welcome-file-list> <!--********************************** log4j configuration ************** -<context-param> <param-name>log4jConfigLocation</param-name> <par Am-value>classpath:config/log4j.properties</param-value> </context-param> <context-param> & Lt;param-name>log4jrefreshinterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframewo rk.web.util.log4jconfiglistener</listener-class> </listener><!--******************************** * * log4j Configuration **********************************-<!--********************************** Spring configuration ********* -<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/springConfig/*.xml</param-value> </context-param> <listener > <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </ listener> <servlet> <servlet-name>xnb_web_api</servlet-name> <servlet-class>org.spring Framework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath:config/project-servlet.xml</param-value> </init-param> <init-param> <PARAM-NAME&G t;encoding</param-name> <param-value>UTF-8</param-value> </init-param> <load-on-star tup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>xnb_web_api</ servlet-name> <url-pattern>/</url-pattern> </servlet-mapping><!--*********************** Spring Configuration **********************************-<!--********************************** Character Set filtering * * * -<filter> <filter-name>set Character Encoding</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</ filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8< ;/param-value> </init-param> <init-parAm> <param-name>forceEncoding</param-name> <param-value>true</param-value></ init-param> </filter> <filter-mapping> <filter-name>set Character encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping><!--********************************** Character Set filtering -<!--********************************** Filter configuration * -<filter> <filter-name>loginCheck</filter-name> < Filter-class>com.moensun.xnbwebapi.filter.sessioncheck</filter-class> </filter> < Filter-mapping> <filter-name>loginCheck</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--********************************** Filter configuration **********************************--> </web-app>
Project-servlet.xml
<?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:p= "http://www.springframework.org/schema/p" Xmlns:con text= "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-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/tx http://www.springframework.org/schema/tx/ Spring-tx-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/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.0.xsd "> <context:annotation-config/> <mvc:annotation-Driven/><!--************************ convert the class labeled @controller annotation to Bean ************************--<context : Component-scan base-package= "Com.moensun.xnbwebapi.controller" > </context:component-scan> <!--* Converts a class labeled @controller annotation to a bean ************************---<mvc:resources mapping= "/resources/**" location= "/resources/"/> <!--static Files--<!--***************************************** Chinese garbled solution to UTF8 output *****************************************--><bean class= "Org.springframework.web.servlet . mvc.annotation.AnnotationMethodHandlerAdapter "> <property name=" messageconverters "> <list> <ref bean= "Jsonhttpmessageconverter"/> <ref bean= "Stringhttpmessageconverter"/> < ;/list> </property></bean><!--JSON--<bean id= "Jsonhttpmessageconverter" class= "Org.sprin Gframework.http.converter.jsoN.mappingjackson2httpmessageconverter "><property name=" Supportedmediatypes "value=" Application/json "/> <property name= "Objectmapper" ref= "Jacksonobjectmapper"/></bean> <bean id= "JacksonObjectMapper" class= "Com.fasterxml.jackson.databind.ObjectMapper"/><bean id= "Jacksonserializationconfig" class= " Com.fasterxml.jackson.databind.SerializationConfig "factory-bean=" Jacksonobjectmapper "factory-method=" Getserializationconfig "/><!--json--><!--string--><bean id=" Stringhttpmessageconverter "class=" Org.springframework.http.converter.StringHttpMessageConverter "><property name=" Supportedmediatypes "> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=utf-8</va lue> <value>application/json;charset=UTF-8</value> </list> </property> </bean>& lt;! --string--><!--***************************************** Chinese garbled solution to UTF8 output -<!--***************************** configuration view interpreter ************* --<bean class= "Org.springframework.web.servlet.view.ContentNegotiatingViewResolver" > <property name= "MediaTypes" > <map> <entry key= "html" value= "text/html"/> <entry key= "xml "Value=" Text/xml "/> <entry key=" json "value=" Application/json "/> </map> </property><proper Ty name= "Defaultviews" > <list> <bean class= "Org.springframework.web.servlet.view.json.MappingJackson 2JsonView "/> </list> </property><property name=" Viewresolvers "> <list> <bean CLA ss= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "viewclass" value= " Org.springframework.web.servlet.view.JstlView "/> <property name=" prefix "value="/web-inf/views/"/> < ;p roperty name= "suffix" value= ". JSP"/> </bean> </list></property></bean><!--***************************** Configuration View Interpreter * * * * -</beans>
Applicationcontext.xml
<?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:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:mongo= "Http://www.springframework.org/schema/data/mongo" xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.0.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/sp Ring-tx-34.0.xsd 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.or G/schema/context/spring-context-4.0.xsd http://www.springframework.org/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/sche Ma/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd "> <mvc: annotation-driven/> <context:component-scan base-package= "Com.moensun.xnbwebapi" ></context: component-scan> <!--******************************************************** loading external files ********************** -<bean class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "><property name=" Systempropertiesmodename "value=" System_properties_mode_override "/> <property name=" IgnoreResourceNotFound " Value= "true"/> <property name= "Ignoreunresolvableplaceholders" value= "true"/><property name= "locations "><list><value>classpath:config/dbconfig/db.properties</value></list></property > </bean><!--******************************Loading external files ********************************************************-in </beans>
Configure as above, the code is as follows
@Controller @requestmapping (value= "user/") public class Usercontroller {@RequestMapping (value= "login") public @ Responsebody Model Login (final model) {Model.addattribute ("1", "2"); return model;}}
With the model of this return, always mapped to the URL, I do not know where the problem, but also can return JSON. If any of the great gods come across, please help me look.
SPRINGMVC configuration, please see the gods, there is no problem, I use the model to do the return type is always mapped to the URL