java web018——SSM
Posted 江州益彤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java web018——SSM相关的知识,希望对你有一定的参考价值。
一、外部依赖
使用的数据库
创建动态web工程,导入需要的jar包
二、文件配置
2.1、在web.xml文件中进行spring核心容器的配置
2.2、创建springspring核心容器的配置文件
先选择bean,context,tx后面需要再添加
2.3、在web.xml文件中进行springMVC的配置
2.4、创建springMVC的配置文件
2.5、在web.xml文件中配置字符编码过滤器
2.6、在web.xml文件中配置restful风格过滤器
2.7、配置springMVC的配置文件springmvc.xml
2.8、配置springMVC的配置文件applicationContext.xml
2.9、创建mybatis-config.xml配置文件
大部分配置在applicationContext.xml中完成
三、工程搭建
3.1、工程结构
3.2、bean
3.3、index.jsp
3.4、GoodsController.java
package com.dgut.ssm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.dgut.ssm.bean.Goods;
import com.dgut.ssm.service.GoodsService;
@Controller
@RequestMapping("/goods")
public class GoodsController
@Autowired
private GoodsService service;
@GetMapping(path="/list")
public String list(Model model)
List<Goods> goods=service.getAllGoods();
model.addAttribute("goods",goods);
return "goodsList";
@GetMapping(path="/edit/id")
public String edit(@PathVariable("id") Integer id,Model model)
Goods goods=service.getGoodsById(id);
//放进模型中
model.addAttribute("goods",goods);
return "goodsEdit";
//所有方法执行前都会先执行该方法
//required = false设置请求路径中非必要,否则不带goodsId的请求会报错
@ModelAttribute
public void prepareModel(@RequestParam(value = "goodsId", required = false) Integer id,HttpServletRequest request,Model model)
//获取goodsEdit.jsp页面传递过来的goodsId
String servletPath =request.getServletPath();
if(servletPath.equals("/goods/update"))
Goods goods=service.getGoodsById(id);
model.addAttribute("modelGoods",goods);
//@ModelAttribute("modelGoods")对应上面的model.addAttribute("modelGoods",goods);
@PutMapping(path="/update")
public String update(@ModelAttribute("modelGoods") Goods goods)
System.out.println("update goods=="+goods);
service.updateGoods(goods);
return "redirect:/goods/list";
3.5、GoodsService.java
3.6、GoodsDao.java
3.7、GoodsMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dgut.ssm.dao.GoodsDao">
<!-- 实现public Goods getGoodsByid(Integer Id)接口 -->
<select id="getGoodsByid" resultType="com.dgut.ssm.bean.Goods">
select goods_id goodsId,name,price,quantity
from tbl_goods
where goods_id=#id
</select>
<!-- 实现public void update(Goods goods)接口 -->
<update id="update" >
update tbl_goods
set name=#name,price=#price,quantity=#quantity
where goods_id=#goodsId
</update>
<!-- 实现public List<Goods> getAllGoods()接口 -->
<select id="getAllGoods" resultType="com.dgut.ssm.bean.Goods">
select goods_id goodsId,name,price,quantity
from tbl_goods
</select>
</mapper>
3.8、goodsList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Goods List Page</h3>
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Update</th>
<th>Delete</th>
</tr>
<c:forEach items="$requestScope.goods " var="g">
<tr>
<td>$g.goodsId </td>
<td>$g.name </td>
<td>$g.price </td>
<td>$g.quantity </td>
<td><a href="$pageContext.request.contextPath/goods/edit/$g.goodsId ">Update</a></td>
<td><a href="$pageContext.request.contextPath/goods/delete">Delete</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
3.9、goodsEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Goods edit page</h3>
<form:form modelAttribute="goods" action="$pageContext.request.contextPath/goods/update" method="post">
<form:hidden path="goodsId"/>
<!-- 解决rest风格,使用常规的input标签 -->
<input type="hidden" name="_method" value="put">
Name:$requestScope.goods.name <br>
Price:<form:input path="price"/><br>
Quantity:<form:input path="quantity"/><br>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
四、运行结果
以上是关于java web018——SSM的主要内容,如果未能解决你的问题,请参考以下文章