一篇文章帶你了解SpringMVC數(shù)據(jù)綁定
參考應(yīng)用ch4創(chuàng)建應(yīng)用practice44。在應(yīng)用practice44中創(chuàng)建兩個(gè)視圖頁(yè)面addGoods.jsp和goodsList.jsp。addGoods.jsp頁(yè)面的顯示效果如圖4.5所示,goodsList.jsp頁(yè)面的顯示效果如圖4.6所示。
圖4.5 添加商品頁(yè)面
圖4.6 商品顯示頁(yè)面
具體要求:
1.商品類型由控制器類GoodsController的方法inputGoods進(jìn)行初始化。GoodsController類中共有三個(gè)方法:inputGoods、addGoods和listGoods。
2.使用Goods模型類封裝請(qǐng)求參數(shù)。
3.使用Service層,在Service的實(shí)現(xiàn)類中,使用靜態(tài)集合變量模擬數(shù)據(jù)庫(kù)存儲(chǔ)商品信息,在控制器類中使用@Autowired注解Service。
4.通過地址http://localhost:8080/practice44/goods/input訪問addGoods.jsp頁(yè)面。
5.其他的注意事項(xiàng)參見應(yīng)用ch4。
1.配置web.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_4_0.xsd" version="4.0"> <servlet> <servlet-name>springmvc_10</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc_servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc_10</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 避免中文亂碼 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.在resources目錄下配springmvc_servlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.sxau2.controller"/> <context:component-scan base-package="com.sxau2.servlet"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.在WEB-INF目錄下新建jsp文件夾
4.在java目錄下新建com.sxau2包并在子目錄下新建Controller、pojo、servlet包
Controller包下新建GoodsController.java
package com.sxau2.controller; import com.sxau2.pojo.Goods; import com.sxau2.servlet.GoodsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; @Controller @RequestMapping("/goods") public class GoodsController { @Autowired private GoodsService goodsService; @RequestMapping("/add") public String add(Model model){ Goods goods = new Goods(); model.addAttribute("goods",goods); model.addAttribute("goodsTypes",new String[]{"電器","食品","家居","數(shù)碼"}); return "goodsAdd"; } @RequestMapping("/save") public String save(@ModelAttribute Goods goods,Model model){ if (goodsService.addGoods(goods)){ return "redirect:/goods/list"; }else return "/goods/add"; } @RequestMapping("/list") public String list(Model model){ ArrayList<Goods> goods = goodsService.listGoods(); System.out.println(goods.toString()); model.addAttribute("listgoods",goods); return "goodsList"; } }
pojo包下新建Goods.java實(shí)體類
package com.sxau2.pojo; public class Goods { String goodsName; String goodsPrice; String goodsType; public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public String getGoodsPrice() { return goodsPrice; } public void setGoodsPrice(String goodsPrice) { this.goodsPrice = goodsPrice; } public String getGoodsType() { return goodsType; } public void setGoodsType(String goodsType) { this.goodsType = goodsType; } @Override public String toString() { return "Goods{" + "goodsName='" + goodsName + '\'' + ", goodsPrice='" + goodsPrice + '\'' + ", goodsType='" + goodsType + '\'' + '}'; } }
service業(yè)務(wù)層新建GoodsService.java
package com.sxau2.servlet; import com.sxau2.pojo.Goods; import java.util.ArrayList; public interface GoodsService { boolean addGoods(Goods goods); ArrayList<Goods> listGoods(); }
service業(yè)務(wù)層新建Impl包并在包下新建GoodsServiceImpl.java
package com.sxau2.servlet.impl; import com.sxau2.pojo.Goods; import com.sxau2.servlet.GoodsService; import org.springframework.stereotype.Service; import java.util.ArrayList; @Service public class GoodsServiceImpl implements GoodsService { private static ArrayList<Goods> goodsList = new ArrayList<Goods>(); @Override public boolean addGoods(Goods goods) { goodsList.add(goods); return true; } @Override public ArrayList<Goods> listGoods() { return goodsList; } }
5.編寫前端首頁(yè)index.jsp頁(yè)面
<%-- Created by IntelliJ IDEA. User: 張晟睿 Date: 2021/6/13 Time: 16:07 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body bgcolor="#ffc0cb"> <p align="center">歡迎來到首頁(yè)</p> <p align="center"><a href="/goods/add">跳轉(zhuǎn)到商品首頁(yè)</a></p> </body> </html>
6.在jsp文件夾下新建goodsAdd.jsp、goodsList.jsp goodsAdd.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%-- Created by IntelliJ IDEA. User: 張晟睿 Date: 2021/6/13 Time: 16:20 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form:form modelAttribute="goods" action="/goods/save" method="post"> <table border="1"> <tr> <td>商品名稱:</td> <td><form:input path="goodsName"/></td> </tr> <tr> <td>商品價(jià)格:</td> <td><form:input path="goodsPrice"/></td> </tr> <tr> <td>商品類型:</td> <td> <form:select path="goodsType"> <option/>請(qǐng)選擇 <form:options items="${goodsTypes}"/> </form:select> </td> </tr> <tr> <td><input type="submit" value="提交"/> </td> <td><input type="reset" value="重置"/> </td> </tr> </table> </form:form> </body> </html>
goodsList.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: 張晟睿 Date: 2021/6/13 Time: 16:20 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>商品展示</h1> <a href="/goods/add">繼續(xù)添加</a> <table border="1"> <tr> <th>商品名稱</th> <th>商品價(jià)格</th> <th>商品類型</th> </tr> <c:forEach items="${listgoods}" var="goods"> <tr> <td>${goods.goodsName}</td> <td>${goods.goodsPrice}</td> <td>${goods.goodsType}</td> </tr> </c:forEach> </table> </body> </html>
7.運(yùn)行注意事項(xiàng):
地址欄:http://localhost:8080/
增加商品欄地址:http://localhost:8080/goods/add
商品展示地址:http://localhost:8080/goods/list
運(yùn)行截圖:
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Spring探秘之如何妙用BeanPostProcessor
BeanPostProcessor也稱為Bean后置處理器,它是Spring中定義的接口,在Spring容器的創(chuàng)建過程中會(huì)回調(diào)BeanPostProcessor中定義的兩個(gè)方法,這篇文章主要給大家介紹了關(guān)于Spring探秘之如何妙用BeanPostProcessor的相關(guān)資料,需要的朋友可以參考下2022-01-01使用IDEA配置Tomcat和連接MySQL數(shù)據(jù)庫(kù)(JDBC)詳細(xì)步驟
這篇文章主要介紹了使用IDEA配置Tomcat和連接MySQL數(shù)據(jù)庫(kù)(JDBC)詳細(xì)步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Java后臺(tái)通過Collections獲取list集合中最大數(shù),最小數(shù)代碼
這篇文章主要介紹了Java后臺(tái)通過Collections獲取list集合中最大數(shù),最小數(shù)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08SpringBoot啟動(dòng)自動(dòng)終止也不報(bào)錯(cuò)的原因及解決
這篇文章主要介紹了SpringBoot啟動(dòng)自動(dòng)終止也不報(bào)錯(cuò)的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java多線程編程實(shí)戰(zhàn)之模擬大量數(shù)據(jù)同步
這篇文章主要介紹了Java多線程編程實(shí)戰(zhàn)之模擬大量數(shù)據(jù)同步,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02