來千鋒學習的第19天
SpringMVC基礎
概念
SpringMVC簡介
配置
添加依賴 Spring-webmvc(4.3.6)八個jar包添加進來
配置web.xml文件: 讓所的/請求都交給了我DispatcherServlet,而DispatcherServlet里面需要配置一個contextConfigLocation,上下文配置路徑,也就是我們的SpringMVC的配置文件。如果沒有顯式地配置該屬性,SpringMVC會在默認WEB-INF下去找[servlet-name]-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring-mvc.xml
<?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: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">
<!--
配置SpringMVC的視圖解析器
可以分別指定前綴和后綴
prefix: /WEB-INF/view/,那么控制器那邊會在虛擬視圖前拼接該字符串
suffix:.jsp .html,那么控制器那邊會在虛擬視圖后面拼接該字符串
拼接完字符串的效果
/WEB-INF/view/index.html
/WEB-INF/view/detail.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<!--<property name="suffix" value=".jsp" />-->
</bean>
<!--配置缺省的servlet處理器,靜態(tài)資源可以直接被訪問-->
<mvc:default-servlet-handler />
<!--
bean的id屬性值不能包含特殊字符
name可以,所以路徑需要使用name來標識一個控制器的路徑
指定name對應路徑交給哪個控制器來進行具體的處理
-->
<bean name="/ProductInput" class="com.qfedu.controller.ProductInputController" />
<bean name="/SaveProductController" class="com.qfedu.controller.SaveProductController" />
</beans>
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請求會交給當前類來進行處理,自動執(zhí)行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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>this is product input html page.</h1>
<form method="post" action="SaveProductController">
pid:<input type="text" name="pid" /><br />
pname:<input type="text" name="pname" /><br />
price:<input type="text" name="price" /><br />
<input type="submit" value="submit" /><br />
</form>
</body>
</html>
在當前的表單頁面上輸入信息提交,交給一個新的請求/SaveProductController,那么發(fā)出一個新的請求,先交給DispatcherServlet,然后讀取SpringMVC配置文件,發(fā)現(xiàn)該請求交給了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);
/**
* 創(chuàng)建模型視圖對象
* 該代碼等同于以前的兩行代碼
* 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" %>
<html>
<head>
<title>detail</title>
</head>
<body>
<h1>this is product detail page.</h1>
<h3>pid : ${p.pid}</h3>
<h3>pname : ${p.pname}</h3>
<h3>price : ${p.price}</h3>
</body>
</html>
沒有人能改變你,別人只能影響你,能改變你的只有你自己。加油!