参考应用ch4创建应用practice44。在应用practice44中创建两个视图页面addGoods.jsp和goodsList.jsp。addGoods.jsp页面的显示效果如图4.5所示,goodsList.jsp页面的显示效果如图4.6所示。
图4.5 添加商品页面
1.商品类型由控制器类GoodsController的方法inputGoods进行初始化。GoodsController类中共有三个方法:inputGoods、addGoods和listGoods。
2.使用Goods模型类封装请求参数。
3.使用Service层,在Service的实现类中,使用静态集合变量模拟数据库存储商品信息,在控制器类中使用@Autowired注解Service。
4.通过地址http://localhost:8080/practice44/goods/input访问addGoods.jsp页面。
5.其他的注意事项参见应用ch4。
<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_10servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc_servlet.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvc_10servlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
<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>
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[]{
"电器","食品","家居","数码"});
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";
}
}
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 + '\'' +
'}';
}
}
package com.sxau2.servlet;
import com.sxau2.pojo.Goods;
import java.util.ArrayList;
public interface GoodsService {
boolean addGoods(Goods goods);
ArrayList<Goods> listGoods();
}
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;
}
}
<%--
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" %>
$Title$
欢迎来到首页
<%@ 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" %>
Title
商品名称:
商品价格:
商品类型:
请选择
<%@ 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" %>
Title
商品展示
继续添加
商品名称
商品价格
商品类型
${goods.goodsName}
${goods.goodsPrice}
${goods.goodsType}