SpringMVC基础
概念
SpringMVC简介
配置
添加依赖 Spring-webmvc(4.3.6)八个jar包添加进来
配置web.xml文件: 让所的/请求都交给了我DispatcherServlet,而DispatcherServlet里面需要配置一个contextConfigLocation,上下文配置路径,也就是我们的SpringMVC的配置文件。如果没有显式地配置该属性,SpringMVC会在默认WEB-INF下去找[servlet-name]-servlet.xml文件
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
dispatcherServlet
/
Spring-mvc.xml
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
ProductInputController.java
package com.qfedu.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
-
SpringMVC配置文件中让/ProductInput请求会交给当前类来进行处理,自动执行Controller的回调方法
*/
public class ProductInputController implements Controller {/**
- /ProductInput请求会回调该方法
- @param request
- @param response
- @return 视图和模型对象,只需要展示页面,则需要一个单纯的虚拟视图名即可
- @throws Exception
*/
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//return new ModelAndView("/WEB-INF/view/index.jsp");
return new ModelAndView("index.html");
}
}
index.html
this is product input html page.
在当前的表单页面上输入信息提交,交给一个新的请求/SaveProductController,那么发出一个新的请求,先交给DispatcherServlet,然后读取SpringMVC配置文件,发现该请求交给了SaveProductController控制器
package com.qfedu.controller;
import com.qfedu.bean.Product;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SaveProductController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String spid = request.getParameter("pid");
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");
int pid = spid == null ? 0 : Integer.parseInt(spid);
double price = sprice==null ? 0.0 : Double.parseDouble(sprice);
Product p = new Product();
p.setPid(pid);
p.setPname(pname);
p.setPrice(price);
/**
* 创建模型视图对象
* 该代码等同于以前的两行代码
* request.setAttribute("p", p);
* request.getRequestDispatcher("detail.jsp").forward(request, response);
*/
ModelAndView mv = new ModelAndView("detail.jsp", "p", p);
return mv;
}
}
detail.jsp
<%--
Created by IntelliJ IDEA.
User: james
Date: 2020/3/3
Time: 11:27 AM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
this is product detail page.
pid : ${p.pid}
pname : ${p.pname}
price : ${p.price}