• 中文
    • English
  • 注册
  • 查看作者
    • SpringMVC:解决乱码和静态资源访问

      一.  数据准备

      test.jsp

      <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
      <!DOCTYPE html>
      <html>
      <head>
          <title>Title</title>
      </head>
      <body>
      
      <form action="${pageContext.request.contextPath}/test" method="post">
          <input type="text" name="name"/>
      
          <input type="submit" value="提交"/>
      </form>
      </body>
      </html>

      Controller

      package io.zhangjia.springmvc.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      
      @Controller
      public class Test {
          @RequestMapping("/test")
          public String test(String name)  {
              System.out.println("name = " + name);
              return "test";
          }
      }

      二.  解决乱码

      在《CSS表单》一文中我们讲过,form表单提交的默认方式是get,如果想修改其提交方式,则需要添加method属性,但是如果提交方式为post,传输中文便可能发生乱码,SpringMVC也为其提供了相关的解决方案,只需要在web.xml文件中添加以下内容即可:

      <filter>
          <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

      三.  静态资源访问

      我们在jsp文件中,加入本地的一张图片:

      <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
      <!DOCTYPE html>
      <html>
      <body>
      <img src="${pageContext.request.contextPath}/static/img/1.jpg">
      </body>
      </html>

      但是此时打开text.jsp,会发现图片无法正常显示,单独打开图片地址,会显示404,这是因为SpringMVC默认把图片的路径也当成了一个页面去处理,解决方法如下:

      方法一,在spring-mvc.xml的beans标签中添加以下内容

          <mvc:default-servlet-handler />
          <mvc:annotation-driven />

      <?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/views/" />
              <property name="suffix" value=".jsp" />
          </bean>
      
          <context:component-scan base-package="io.zhangjia.springmvc" />
          
          <mvc:default-servlet-handler />
          <mvc:annotation-driven />
      </beans>

      方法二:

      <?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/views/" />
              <property name="suffix" value=".jsp" />
          </bean>
      
          <context:component-scan base-package="io.zhangjia.springmvc" />
      <!--/**代表匹配static下的所有目录,无论几级-->
      <!--    <mvc:resources mapping="/static/**" location="/static/" />-->
          <mvc:annotation-driven />
      </beans>

      如果不添加<mvc:annotation-driven />,那么静态资源可以访问,但是Controller的请求无法访问

      四.  总结

      1.  解决乱码

      • 在web.xml中添加filter、filter-mapping标签,并进行相关配置

      2.  访问静态资源

      • 方法一:在spring-mvc.xml中添加

       <mvc:default-servlet-handler />
       <mvc:annotation-driven />
      • 方法二:在spring-mvc.xml中添加

       <mvc:resources mapping="/static/**" location="/static/" />
       <mvc:annotation-driven />

    • 0
    • 0
    • 0
    • 1.3k
    • 请登录之后再进行评论

      登录

      赞助本站

      • 支付宝
      • 微信
      • QQ

      感谢一直支持本站的所有人!

      单栏布局 侧栏位置: