验证中...
助力高校计算机教育 —— 码云为老师推出免费高校版,高达 200 人的协作团队
语言: Java
分类: JavaScript 工具
最后更新于 2018-06-14 19:17
片段 1 片段 2 片段 3 片段 4 片段 5
goods_list.jsp
原始数据 复制代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h3>商品管理列表</h3>
<table>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>类别</td>
<td>操作</td>
</tr>
<c:forEach items="${requestScope.goods}" var="good">
<tr>
<td>${good.goods_name}</td>
<td>${good.goods_price}</td>
<td>${good.goodstype.name}</td>
<td>
<a href="${pageContext.request.contextPath}/admin/goods/del.do?id=${good.id}">删除</a>
<a href="${pageContext.request.contextPath}/admin/goods/toedit.do?id=${good.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
goods_toadd.jsp
原始数据 复制代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h3>添加商品</h3>
<form action="${pageContext.request.contextPath}/admin/goods.do?op=doadd" method="post">
商品名称:<input name="goods_name" type="text" /><br/> 商品类别:
<select name="typeid">
<option value="0">请选择</option>
<c:forEach items="${requestScope.types}" var="t">
<option value="${t.id}">${t.name}</option>
</c:forEach>
</select> <br/> 商品价格:
<input name="goods_price" type="text" /><br/> 商品描述:
<textarea name="goods_desc" rows="8" cols="40"></textarea><br/>
<input name="btnadd" type="submit" value="添加" />
</form>
</body>
</html>
GoodsDaoImpl.java
原始数据 复制代码
package com.gsdsxsys.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.gsdsxsys.dao.GoosDao;
import com.gsdxsys.entity.Goods;
public class GoodsDaoImpl extends MybatisBaseDao implements GoosDao{
@Override
public List<Goods> selectAll(SqlSession session) {
List<Goods> goodses= super.selectList(session, "GoodsMapper.selectAll");
return goodses;
}
@Override
public int insertGoods(SqlSession session, Goods goods) {
int result= super.insert(session, "GoodsMapper.inserGoods", goods);
return result;
}
}
MyBatisUtis
原始数据 复制代码
package com.gsdsxsys.dao.impl;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtis {
private static SqlSessionFactory sqlSessionFactory = null;
public static SqlSession createSqlSession() {
SqlSession session = null;
if (sqlSessionFactory == null) {
String resource = "config/Configuration.xml";
Reader reader;
SqlSessionFactory sqlSessionFactory = null;
try {
reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
session = sqlSessionFactory.openSession();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
session = sqlSessionFactory.openSession();
}
return session;
}
}
GoodsServlet.java
原始数据 复制代码
package com.gsdxsys.admin.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gsdxsys.entity.Goods;
import com.gsdxsys.entity.GoodsType;
import com.gsdxsys.service.GoodsService;
import com.gsdxsys.service.GoodsTypeService;
import com.gsdxsys.service.impl.GoodsServiceImpl;
import com.gsdxsys.service.impl.GoodsTypeServiceImpl;
public class GoodsServlet extends HttpServlet {
private GoodsService service=new GoodsServiceImpl();
private GoodsTypeService typeservice=new GoodsTypeServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String action=request.getParameter("op");
if("toadd".equals(action)){
toadd(request, response);
}else if("doadd".equals(action)){
doadd(request, response);
}else if("list".equals(action)){
list(request, response);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
protected void toadd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<GoodsType> types= typeservice.getAll();
req.setAttribute("types", types);
req.getRequestDispatcher("/admin/goods_toadd.jsp").forward(req, resp);
}
protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Goods> goods= service.getAll();
req.setAttribute("goods", goods);
req.getRequestDispatcher("/admin/goods_list.jsp").forward(req, resp);
}
protected void doadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String goods_name=request.getParameter("goods_name");
int typeid=Integer.parseInt(request.getParameter("typeid"));
double goods_price=Double.parseDouble(request.getParameter("goods_price"));
String goods_desc=request.getParameter("goods_desc");
Goods goods=new Goods();
goods.setGoods_desc(goods_desc);
goods.setGoods_name(goods_name);
goods.setGoods_price(goods_price);
GoodsType goodstype=new GoodsType();
goodstype.setId(typeid);
goods.setGoodstype(goodstype);
int result= service.addGoods(goods);
if(result>0){
response.sendRedirect(request.getContextPath()+"/admin/goods.do?op=list");
}else{
response.getWriter().println("添加失败");
}
}
}

评论列表( 0 )

你可以在登录后,发表评论

10_float_left_people 10_float_left_close