Spring Mvc笔记
SpringMvc 简单示例
- web.xml 配置
<!--注册dispatcherservlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的的请求:(不包括.jsp)-->
<!--/* 匹配所有的的请求:(包括.jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- springmvc-servlet.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<?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-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.xsd">
<!-- 自动扫描包,让指定包下的注解生效,由ioc容器统一管理-->
<context:component-scan base-package="com.zeng.controller"/>
<!-- 让spring mvc不处理静态资源-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- controller类编写
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 模型和视图
ModelAndView mv = new ModelAndView();
// 封装对象,放在ModelAndView中
mv.addObject("msg","HelloSpringMvc");
// 封装要跳转的视图,放在ModelAndView中
/*
* 可能遇到的404
* 查看项目目录接口是否存在lib目录,不存在新建然后添加所有的jar包,重启即可
* prioject -> structure -> project setting -> Artifacts -> 选择对应的项目在项目WEB-INF目录下新建lib目录 -> Library files
*/
mv.setViewName("hello");
return mv;
}
}
可能遇到的问题
- 可能遇到的404
- 查看项目目录接口是否存在lib目录,不存在新建然后添加所有的jar包,重启即可
- Prioject Structure -> Project Setting -> Artifacts -> 选择对应的项目在项目WEB-INF目录下新建lib目录 -> Library files
乱码问题
可通过下面四个方法解决
- 直接用springmvc配置
<!--配置SpringMvc的乱码过滤-->
<filter>
<filter-name>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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 自定义filter过滤器
public class EncodingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {
}
}
- 修改tomcat配置
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="utf-8"/>
- 百度找大佬写的过滤器
Json
前端js对象和json对象互转
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
var user = {
name:"小小飞",
age:19,
sex:"男"
}
// 将js对象转为json
var s = JSON.stringify(user);
console.log(s);
// 将json对象转为js对象
var parse = JSON.parse(s);
console.log(parse)
// 可直接调用单个属性
console.log(parse.name,user.age)
</script>
</head>
返回的json乱码问题处理,servlet-config.xml添加配置
<!--json乱码问题处理-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
个人遇到的问题
整合mvc
导入的aspectjweaver高于下面的版本就报异常了:java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldExcep…,我也不知道为撒,反正用下面的版本就ok
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>