先搞一波效果图
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有效
*
* 第一次获取请求参数之前: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(最重要的就是设置那个字符编码过滤器)
springmvc_Day01
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
springmvc
org.springframework.web.servlet.DispatcherServlet
1
springmvc
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceRequestEncoding
true
forceResponseEncoding
true
CharacterEncodingFilter
/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*