SpringMVC基礎(chǔ)

SpringMVC基礎(chǔ)
概念
SpringMVC簡(jiǎn)介
配置
添加依賴 Spring-webmvc(4.3.6)八個(gè)jar包添加進(jìn)來

配置web.xml文件: 讓所的/請(qǐng)求都交給了我DispatcherServlet,而DispatcherServlet里面需要配置一個(gè)contextConfigLocation,上下文配置路徑,也就是我們的SpringMVC的配置文件。如果沒有顯式地配置該屬性,SpringMVC會(huì)在默認(rèn)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/,那么控制器那邊會(huì)在虛擬視圖前拼接該字符串
            suffix:.jsp .html,那么控制器那邊會(huì)在虛擬視圖后面拼接該字符串

            拼接完字符串的效果
                /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來標(biāo)識(shí)一個(gè)控制器的路徑
        指定name對(duì)應(yīng)路徑交給哪個(gè)控制器來進(jìn)行具體的處理
-->
<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請(qǐng)求會(huì)交給當(dāng)前類來進(jìn)行處理,自動(dòng)執(zhí)行Controller的回調(diào)方法
    */
    public class ProductInputController implements Controller {

    /**

    • /ProductInput請(qǐng)求會(huì)回調(diào)該方法
    • @param request
    • @param response
    • @return 視圖和模型對(duì)象,只需要展示頁面,則需要一個(gè)單純的虛擬視圖名即可
    • @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>

在當(dāng)前的表單頁面上輸入信息提交,交給一個(gè)新的請(qǐng)求/SaveProductController,那么發(fā)出一個(gè)新的請(qǐng)求,先交給DispatcherServlet,然后讀取SpringMVC配置文件,發(fā)現(xiàn)該請(qǐng)求交給了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)建模型視圖對(duì)象
     *  該代碼等同于以前的兩行代碼
     *          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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容