配置:
spring-mvc.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: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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 注解扫描包:注入所有的注解包 -->
<context:component-scan base-package="com.tgb" />
<!-- 开启注解 -->
<mvc:annotation-driven />
<!-- 不能同时访问静态资源和动态资源的问题 -->
<mvc:default-servlet-handler/>
<!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 文件上传配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="100242880" />
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
页面返回异常json提示
/**
* 控制器内部的异常处理
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler(Exception.class)
public Result handleException(Exception e) {
System.out.println("打印");
return new Result(ResultCode.WEAK_NET_WORK);
}
尴尬的发现返回不了json。报异常:
Could not find acceptable representation
经查阅资料发现存在两种可能:
返回的实体对象没有setter或者getter
没有提供序列化的包,org.codehaus.jackson 和 com.fasterxml.jackson.core 至少提供一个. 建议使用后者。
看了下依赖,发现缺少jar包,引入后就可以了。
jackson-annotations-2.5.0.jar
jackson-core-2.5.0.jar
jackson-databind-2.5.0.jar