项目结构
web.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.jsp</welcome-file> </welcome-file-list> <!--********************************** log4j配置 ********************************** --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:config/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!--********************************** log4j配置 ********************************** --> <!--********************************** spring配置 ********************************** --> <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.springframework.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>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>xnb_web_api</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--********************************** spring配置 ********************************** --> <!-- ********************************** 字符集过滤 ********************************************************* --> <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> <!-- ********************************** 字符集过滤 ********************************************************* --> <!-- **********************************过滤器配置 **********************************--> <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> <!-- **********************************过滤器配置 **********************************--> </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: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-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 /> <!-- ************************ 把标记了@Controller注解的类转换为bean ************************ --> <context:component-scan base-package="com.moensun.xnbwebapi.controller"> </context:component-scan> <!-- ************************ 把标记了@Controller注解的类转换为bean ************************ --> <mvc:resources mapping="/resources/**" location="/resources/"/> <!-- 静态文件 --> <!--***************************************** 中文乱码解决 以UTF8输出 *****************************************--> <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.springframework.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</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- string --> <!--***************************************** 中文乱码解决 以UTF8输出 *****************************************--> <!-- ***************************** 配置视图解释器 ***************************** --> <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> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> </list> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> </bean> <!-- ***************************** 配置视图解释器 ***************************** --> </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/spring-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.org/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/schema/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> <!--******************************************************** 载入外部文件 ******************************************************** --> <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> <!--******************************************************** 载入外部文件 ******************************************************** --> </beans>
配置如上 ,代码如下
@Controller @RequestMapping(value="user/") public class UserController { @RequestMapping(value="login") public @ResponseBody Model login(final Model model){ model.addAttribute("1","2"); return model; } }
用Model这种返回方式,总是映射到URL上,我不知道哪里出了问题,同时是可以返回JSON的。有哪位大神遇到请帮我看下。
时间: 2024-10-07 21:51:16