02,SpringMVC请求路径,请求方式及传参

(1),请求路径映射,请求方式GET或POST,浏览器地址传参

import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;

/**
 * @date: 2022/10/30 07:12
 * @desc: 测试控制类
 */
@Controller
@RequestMapping(value = "/studentConter")
public class StudentController {
    // 路径映射
    @RequestMapping(value = "/studentQuery", method = RequestMethod.GET) // 或RequestMethod.POST方式
    public ModelAndView studentQuery(HttpServletRequest request) {
        System.out.println("浏览器客户端请求到服务器的request.getParameter参数="+ request.getParameter("stuName"));

        // 跳转页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/testExamp/studentListJsp");
        return modelAndView;
    }
}

(1.1),浏览器地址访问及传参

http://localhost:8085/mave_wf/studentConter/studentQuery?stuName=张三

(2),请求方式POST表单方式传参,并自动装入实体对象

import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;

/**
 * @date: 2022/10/30 07:12
 * @desc: 测试控制类
 */
@Controller
@RequestMapping(value = "/studentConter")
public class StudentController {
    @RequestMapping(value = "/studentQueryForm", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public ModelAndView studentQueryForm(HttpServletRequest request, @ModelAttribute StudentVo studentVo) {
        System.out.println("打印页面表单提交的数据,如姓名:"+ studentVo.getsName());
        System.out.println("打印页面表单提交的数据,如年龄:"+ studentVo.getsAge());

        // 跳转页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/testExamp/studentListJsp");
        return modelAndView;
    }
}

(2.1),实体类

/**
 * @date: 2022/10/30 19:52
 * @desc: 学生实体类
 */
public class StudentVo implements Serializable {
    private String sName = "";
    private Integer sAge = 0;
    private StudentFshu studentFshu = null;
	// 注意自己去生成属性。。。
}

(2.2),表单提交数据,自动装入实体对象

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String contextPath = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



    
    


    请求并响应到的第一个jsp页面。
    
姓名: 年龄:

(3),表单提交数据,提交复杂实体对象(即实体对象包含另一个实体对象)

import javax.servlet.http.HttpServletRequest;
/**
 * @date: 2022/10/30 07:12
 * @desc: 测试控制类
 */
@Controller
@RequestMapping(value = "/studentConter")
public class StudentController {
    // 路径映射
    @RequestMapping(value = "/studentQueryFormFshu", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public ModelAndView studentQueryFormFshu(HttpServletRequest request, @ModelAttribute StudentVo studentVo) {
        System.out.println("打印页面表单提交的数据,如姓名:"+ studentVo.getsName());
        System.out.println("打印页面表单提交的数据,如附属姓名:"+ studentVo.getStudentFshu().getFshuName() );

        // 跳转页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/testExamp/studentListJsp");
        return modelAndView;
    }
}

(3.1),附属实体对象(被包含的实体对象,主实体是2.1实体对象)

import java.io.Serializable;
/**
 * @date: 2022/10/30 20:53
 * @desc: 学生实体类的附属
 */
public class StudentFshu implements Serializable {
    private String fshuName;
    public String getFshuName() {
        return fshuName;
    }
    public void setFshuName(String fshuName) {
        this.fshuName = fshuName;
    }
}

(3.2),表单提交数据,提交复杂实体对象的jsp页面代码

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String contextPath = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



    
    


    
姓名: 附属姓名:

你可能感兴趣的:(servlet,java,前端)