• SpringMVC之springmvc原始api,请求中文乱码问题


    先搞一波效果图

     1.Controller

    package com.tz.controller;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.beans.factory.annotation.Required;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.CookieValue;
    import org.springframework.web.bind.annotation.RequestHeader;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import com.tz.domain.Course;
    
    @Controller
    public class RequestController {
    	/**
    	 * @PathVariable("user"):获取路径中的值/handler/{user}
    	 * @RequestParam("user"):获取请求参数/hander/{user}?user=coco
    	 * 
    	 * @param userName
    	 * @return
    	 */
    	@RequestMapping("/handle")
    	public String handler(@RequestParam(value="user",required=false) String userName){
    		//userName = request.getParameter("user");
    		System.out.println(userName);
    		return "success";
    	}
    	
    	@RequestMapping("/handle2")
    	public String handler02(@RequestParam(value="user",required=false,defaultValue="你没有带参数") String userName,
    							@RequestHeader(value="User-Agent",required=false) String user_Agent,
    							@CookieValue("JSESSIONID") String js){
    		//userName = request.getParameter("user");
    		System.out.println(userName);
    		System.out.println(user_Agent);
    		System.out.println(js);
    		return "success";
    	}
    	
    	
    	/**
    	 * springmvc可以在参数上写servlet常用的api
    	 * HttpServletRequest
    	 * HttpServletResponse
    	 * Httpsession
    	 * 
    	 * InputStream:request.getInputStream();
    	 * OutputStream:response.getOutputStream();
    	 * Reader:request.getReader();
    	 * Writer:response.getWriter();
    	 * 
    	 * 请求:客户端-->服务端(客户端显示乱码)
    	 * 修改tomcat配置文件
    	 * GET有效
    	 * <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    	 * 第一次获取请求参数之前:request.setCharacterEncoding("UTF-8");
    	 * 
    	 * 响应:服务端-->客户端(服务端显示乱码)
    	 * reponse.setContentType("text/html;charset=utf-8");
    	 * @param c
    	 * @return
    	 */
    	@RequestMapping("/addCourse")
    	public String addCourse(Course c){//springMVC会自动为我们定义的类型进行赋值
    		/**
    		 * 1.将对象中的每一个属性从request参数中尝试取出来,并进行封装
    		 * request.getParameter("参数名");如果有值,就会取出来封装到实体类的属性中(调用set方法)
    		 */
    		System.out.println(c);
    		return "success";
    	}
    	
    	@RequestMapping("/handle04")
    	public String handle04(HttpSession session,HttpServletRequest request){
    		session.setAttribute("session","session域");
    		request.setAttribute("request","request域");
    		return "success";
    	}
    	
    	
    }
    

      2.配置文件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_2_5.xsd" version="2.5">
      <display-name>springmvc_Day01</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <servlet>
      	<servlet-name>springmvc</servlet-name>
      	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      	<!-- <init-param>
      		<param-name>contextConfigLocation</param-name>
      		指定springmvc配置文件的位置
      		<param-value>classpath:springmvc.xml</param-value>
      	</init-param> -->
      	<!-- 如果在web.xml不指定文件位置,也会找默认的文件
      	/WEB-INF/xxx-servlet.xml
      	 -->
      	<!-- 启动加载时期 -->
      	<!-- servlet是当服务器启动的时候加载创建对象,值越小,代表优先级越高,就越先创建对象 -->
    <!--   	<init-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>classpath:springmvc.xml</param-value>
      	</init-param> -->
      	<!-- 如果在web.xml中找不到文件位置,也会默认找文件
      		/WEB-INF/xxx-servlet.xml
      	 -->
      	<load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
      	<servlet-name>springmvc</servlet-name>
      	<url-pattern>/</url-pattern>
      		<!-- 
      	/和/*都是用来拦截所有请求,但是/*的范围更大,还会拦截到*.jsp请求,一旦拦截,则访问jsp页面就不会显示了
      	/也会拦截所有请求,但是不会拦截.jsp,可以保证jsp页面访问正常
      	
      	*.do *.action *.hah
      	/:不拦截jsp *.jsp
      	/*:拦截jsp
      	 -->
      </servlet-mapping>
      
      
      <!-- 配置字符编码过滤器 
      
      	保证该过滤器在所有的过滤器之前,避免请求后拦截不了
      	最好在第一个
      -->
      <filter>
      	<filter-name>CharacterEncodingFilter</filter-name>
      	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      	  	<!-- encoding:指定解决请求乱码
      	  		forceRequestEncoding:指定请求与响应的编码集格式为eccoding
      	  		forceResponseEncoding
      	  	 -->
      	  	<init-param>
      			<param-name>encoding</param-name>
      			<param-value>UTF-8</param-value>
      		</init-param>
      		<init-param>
      			<param-name>forceRequestEncoding</param-name>
      			<param-value>true</param-value>
      		</init-param>
      		<init-param>
      			<param-name>forceResponseEncoding</param-name>
      			<param-value>true</param-value>
      		</init-param>
      	
      </filter>
      <filter-mapping>
      	<filter-name>CharacterEncodingFilter</filter-name>
      	<url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <!-- reset风格的请求
      	get 查询
      	put 请求修改
      	delect删除
      	post新增
       -->
      <filter>
      	<filter-name>HiddenHttpMethodFilter</filter-name>
      	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    
      </filter>
      <filter-mapping>
      	<filter-name>HiddenHttpMethodFilter</filter-name>
      	<!-- 这里将.jsp也过滤掉 -->
      	<url-pattern>/*</url-pattern>
      </filter-mapping>
      
    </web-app>
    

      

  • 相关阅读:
    狗狗急性肠胃炎
    shell change password
    男职工为什么要交生育保险
    预扣预缴个税
    转《最优状态机》
    状态机编程-队列缓冲事件,事件可异步触发
    max713镍氢电池充电管理IC
    NUP2201MR
    LSP5513
    74HC14D(6反向施密特触发器)
  • 原文地址:https://www.cnblogs.com/luyuan-chen/p/11681839.html
Copyright © 2020-2023  润新知