springMvc19-RequestHeader(获取请求头中某一部分值

简介: springMvc19-RequestHeader(获取请求头中某一部分值

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_3_0.xsd" id="WebApp_ID" version="3.0">
         <display-name>SpringMVC01</display-name>
         <!-- 处理中文乱码 -->
         <filter>
             <filter-name>encodingFilter</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>encodingFilter</filter-name>
             <url-pattern>/*</url-pattern>
         </filter-mapping>
         <!-- SpringMVC控制器 -->
         <servlet>
             <servlet-name>dispatcherServlet</servlet-name>
             <!-- 主要就是DispatcherServlet这个servlet起到分发的作用,对请求进行控制分发 -->
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <init-param>
                 <!-- 每个springmvc项目都要一个springmvc项目配置位置,下面配置springmvc配置文件的路径 -->
                 <param-name>contextConfigLocation</param-name>
                 <param-value>/WEB-INF/springMVC-servlet.xml</param-value>
             </init-param>
             <!-- 当容器启动时立即启动 -->
             <load-on-startup>1</load-on-startup>
         </servlet>
         <servlet-mapping>
             <servlet-name>dispatcherServlet</servlet-name>
             <!-- 下面配置springmvc的过滤分发请求类型,可以是/ 或者*.action等 -->
             <url-pattern>/</url-pattern>
         </servlet-mapping>
     </web-app>springmvc-servlet
    <?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.1.xsd
             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
         <!-- 定义要扫描 controller的包-->
         <context:component-scan base-package="wormday.springmvc.helloworld" />
         <mvc:default-servlet-handler />
         <!-- 启动注解驱动 SpringMVC 功能 -->
         <mvc:annotation-driven />
         <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
         <!--指定视图解析器-->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <!-- 视图的路径 -->
             <property name="prefix" value="/WEB-INF/"/>
             <!-- 视图名称后缀  -->
             <property name="suffix" value=".jsp"/>
         </bean>
     </beans>HIcontroller类
    package wormday.springmvc.helloworld;
     import org.springframework.stereotype.Controller;
     import org.springframework.ui.Model; // 这里导入了一个Model类
     import org.springframework.web.bind.annotation.*;
     @Controller
     @RequestMapping("/hi")
     public class HiController {
         @RequestMapping("/say")
         public String say(Model model) { // 参数中传入Model
             model.addAttribute("name","wormday"); // 指定Model的值
             model.addAttribute("url","http://www.cnblogs.com/wormday/p/8435617.html"); // 指定Model的值
             return "say";
         }
         @RequestMapping("/loginForm")
         public String loginForm(){
             return "login";
         }
         @RequestMapping("/hi")
         public String loginFor(){
             return "hi";
         }
         @RequestMapping(value = "/login",method = RequestMethod.POST)
         public String loginXS(String username, String password){
             System.out.println("执行登录");
             System.out.println("username"+username);
             System.out.println("password"+password);
             return "redirect:hi";
         }
         @RequestMapping("/testcase")
         public String testCookie(@CookieValue("JSESSIONID") String sessionId){
             System.out.println(sessionId);
             return "success";
         }
         @RequestMapping("/testHeader")
         public String testHeader(@RequestHeader(value = "User-Agent") String header){
             System.out.println(header);
             return "success";
         }
     }usercontroller类
    package wormday.springmvc.helloworld;
     import org.springframework.stereotype.Controller;
     import org.springframework.web.bind.annotation.RequestMapping;
     import org.springframework.web.bind.annotation.RequestMethod;
     import org.springframework.web.bind.annotation.RequestParam;
     @Controller
     @RequestMapping("/hi")
     //@RequestMapping("/list")
     public class UserController {
        /* @RequestMapping(value = "/list",method = RequestMethod.GET)
         public String login(String username,String password){
             System.out.println("方法1:参数直接获取");
             System.out.println("username:"+username);
             System.out.println("password:"+password);
             return "list";
         }*/
         @RequestMapping(value = "/list",method = RequestMethod.GET)
        public String listForm(
             @RequestParam(value = "currentpage",required = false,defaultValue = "1")Integer currentpage,
             @RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize){
             System.out.println("currentpage"+currentpage);
             System.out.println("pagesize"+pagesize);
             return "list";
         }
     }say.jsp
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     <html>
     <head>
       <title>Title</title>
     </head>
     <body>
     hello world,${name}
     <br/>${url}</body>
     </html>
     复制代码login.jsp
    <%--
       Created by IntelliJ IDEA.
       User: geyao
       Date: 2019/11/6
       Time: 19:57
       To change this template use File | Settings | File Templates.
     --%>
     <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     <html>
     <head>
         <title>Title</title>
     </head>
     <body>
     <form action="hi" method="post">
         <label for="username">用户名<input type="text" id="username" name="username"></label>
         <label for="password">密码<input type="text" id="password" name="password"></label>
         <button>登录</button>
     </form>
     </body>
     </html>hi.jsp
    <%--
       Created by IntelliJ IDEA.
       User: geyao
       Date: 2019/11/6
       Time: 20:17
       To change this template use File | Settings | File Templates.
     --%>
     <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     <html>
     <head>
         <title>Title</title>
     </head>
     <body>
     我是歌谣,登录成功
     </body>
     </html>list.jsp
    <%--
       Created by IntelliJ IDEA.
       User: geyao
       Date: 2019/11/6
       Time: 19:57
       To change this template use File | Settings | File Templates.
     --%>
     <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     <html>
     <head>
         <title>Title</title>
     </head>
     <body>
     <h1>我是表单</h1>
     </body>
     </html>success.jsp
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     <html>
     <head>
         <title>Title</title>
     </head>
     <body>
     成功</body>
     </html>

image.png

相关文章
|
6月前
|
前端开发 Java API
头头是道:揭示Spring MVC如何获取和处理请求头数据
头头是道:揭示Spring MVC如何获取和处理请求头数据
109 1
springMvc20-testparam请求参数和请求头表达式
springMvc20-testparam请求参数和请求头表达式
56 0
springMvc20-testparam请求参数和请求头表达式
|
前端开发 Java Spring
Spring MVC获取HTTP请求头的两种方式小结
这篇文章主要介绍了Spring MVC获取HTTP请求头的两种方式小结,帮助大家更好的理解和使用Spring MVC,感兴趣的朋友可以了解下
1037 0
|
6月前
|
设计模式 前端开发 JavaScript
Spring MVC(一)【什么是Spring MVC】
Spring MVC(一)【什么是Spring MVC】
|
5月前
|
设计模式 前端开发 Java
【Spring MVC】快速学习使用Spring MVC的注解及三层架构
【Spring MVC】快速学习使用Spring MVC的注解及三层架构
77 1
|
5月前
|
前端开发 Java 应用服务中间件
Spring框架第六章(SpringMVC概括及基于JDK21与Tomcat10创建SpringMVC程序)
Spring框架第六章(SpringMVC概括及基于JDK21与Tomcat10创建SpringMVC程序)
|
6月前
|
前端开发 Java 关系型数据库
基于ssm框架旅游网旅游社交平台前后台管理系统(spring+springmvc+mybatis+maven+tomcat+html)
基于ssm框架旅游网旅游社交平台前后台管理系统(spring+springmvc+mybatis+maven+tomcat+html)
|
5月前
|
XML Java 数据格式
SpringMVC的XML配置解析-spring18
SpringMVC的XML配置解析-spring18
|
5月前
|
应用服务中间件
从代码角度戳一下springMVC的运行过程-spring16
从代码角度戳一下springMVC的运行过程-spring16
|
前端开发 Java Go
Spring MVC 和 Spring Boot 的区别
Spring MVC 和 Spring Boot 的区别
219 0