spring mvc学习笔记呀

本文介绍了SpringMvc的基本配置,包括web.xml与springmvc-servlet.xml,处理乱码和Json问题的方法。还分享了解决404和Json对象转换的实战步骤,以及常见问题的排查技巧。
摘要由CSDN通过智能技术生成

SpringMvc 简单示例

  1. 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>
  1. 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>
  1. 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

乱码问题

可通过下面四个方法解决

  1. 直接用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>
  1. 自定义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() {

    }
}
  1. 修改tomcat配置
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
			   URIEncoding="utf-8"/>
  1. 百度找大佬写的过滤器

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值