这个和添加信息的时候是一个问题还是不能在控制台输出,页面跳转也是失败的
添加信息的时候就是System.out.println("接收的数据为:"+cname+" "+cdesc);了但是不能在控制台输出到现在我都不知道什么原因,而且现在编辑操作的时候也是这个样子,代码完全按着教程敲的,clean过,重启过n次了,这两个问题麻烦都帮我看一下,我实在是找不到问题所在了。也就下面这几个可能有错误别的都是自动生成的了
package com.imooc.web.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.domain.Category;
import com.imooc.service.CategoryService;
import com.imooc.service.impl.CategoryServiceImpl;
/**
* Servlet implementation class CategoryServlet
*/
@WebServlet("/CategoryServlet")
public class CategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//请求路径:localhost:8080/shop/CategoryServlet?method=findAll
String methodName=request.getParameter("method");
if("findAll".equals(methodName)) {
//查询所有分类
findAll(request,response);
//跳转到添加页面
}else if("saveUI".equals(methodName)) {
saveUI(request,response);
//保存分类的方法
}else if("save".equals(methodName)) {
save(request,response);
}else if("eidt".equals(methodName)) {
edit(request,response);
}
}
//后台分类管理编辑分类方法
private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接收数据
Integer cid=Integer.parseInt(request.getParameter("cid"));
//2.调用业务层处理数据
CategoryService categoryService=new CategoryServiceImpl();
Category category=categoryService.findOne(cid);
//3.页面跳转
request.setAttribute("category", category);
request.getRequestDispatcher("/admin/category_update.jsp").forward(request,response);
}
//后台分类管理,保存分类的方法
private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
//1.接受数据
String cname=request.getParameter("cname");
String cdesc=request.getParameter("cdesc");
System.out.println("接收的数据为:"+cname+" "+cdesc);
//2.封装数据
Category category=new Category();
category.setCname(cname);
category.setCdesc(cdesc);
//3.调用业务层处理数据
CategoryService categoryService=new CategoryServiceImpl();
categoryService.save(category);
//4.页面跳转
response.sendRedirect(request.getContextPath()+"/CategoryServlet?method=findAll");
}
//后台分类管理跳转到添加页面
private void saveUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/admin/category_add.jsp").forward(request, response);
}
//后台分类管理查询所有分类的方法
private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受参数
//封装数据
//调用业务成处理数据
System.out.println("CategoryServlet的findALL方法执行了");
CategoryService categoryService=new CategoryServiceImpl();
List<Category> list=categoryService.findAll();
for(Category category:list){
System.out.println(category);
}
//页面跳转
request.setAttribute("list", list);
//请求转发
request.getRequestDispatcher("/admin/category_list.jsp").forward(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.imooc.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.imooc.dao.CategoryDao;
import com.imooc.domain.Category;
import com.imooc.utils.JDBCUtils;
public class CategoryDaoImpl implements CategoryDao {
@Override
public List<Category> findAll() {
System.out.println("CategoryDaoImp的findALL方法执行了");
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List<Category> list=null;
try {
//1.处理连接
con=JDBCUtils.getConnection();
//2.编写sql语句
String sql="select*from category";
//3.预编译sql
pstmt=con.prepareStatement(sql);
//4.设置参数
//5.执行sql
rs=pstmt.executeQuery();
//6.结果处理
list=new ArrayList<Category>() ;
while(rs.next()) {
Category category=new Category();
category.setCid(rs.getInt("cid"));
category.setCname(rs.getString("cname"));
category.setCdesc(rs.getString("cdesc"));
list.add(category);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
//7.释放资源
JDBCUtils.release(rs, pstmt, con);
}
return list;
}
@Override
public void save(Category category) {
Connection con=null;
PreparedStatement pstmt=null;
try {
con=JDBCUtils.getConnection();
String sql="insert into category values(null,?,?)";
pstmt=con.prepareStatement(sql);
//设置参数
pstmt.setString(1,category.getCname());
pstmt.setString(2,category.getCdesc());
pstmt.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(pstmt, con);
}
}
@Override
public Category findOne(Integer cid) {
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
con=JDBCUtils.getConnection();
String sql="select*from category where cid=?";
pstmt=con.prepareStatement(sql);
pstmt.setInt(1, cid);
rs=pstmt.executeQuery();
if(rs.next()) {
Category category=new Category();
category.setCid(rs.getInt("cid"));
category.setCname(rs.getString("cname"));
category.setCdesc(rs.getString("cdesc"));
return category;
}
}catch(Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.release(rs, pstmt, con);
}
return null;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<!-- Mirrored from admindesigns.com/demos/absolute/1.1/admin_forms-validation.html by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 06 Aug 2015 02:56:15 GMT -->
<head>
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<title>油画商城--添加分类</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/skin/default_skin/css/theme.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/css/admin-forms.css">
<link rel="shortcut icon" href="${pageContext.request.contextPath }/assets/img/favicon.ico">
</head>
<body class="admin-validation-page" data-spy="scroll" data-target="#nav-spy" data-offset="200">
<div id="main">
<%@ include file="header.jsp" %>
<%@ include file="left.jsp" %>
<section id="content_wrapper">
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 添加分类 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<form id="admin-form" name="addForm" action="${pageContext.request.contextPath }/CategoryServlet?method=save" method="post">
<div class="panel-body bg-light">
<div class="section-divider mt20 mb40">
<span> 基本信息 </span>
</div>
<div class="section row">
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
名称
</label>
</label>
</div>
<div class="col-md-6">
<label for="sn" class="field">
<input id="sn" name="cname" class="gui-input" placeholder="名称" type="text" value="imooc"/>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
描述
</label>
</label>
</div>
<div class="col-md-6">
<label for="address" class="field">
<input id="address" name="cdesc" class="gui-input" placeholder="描述" type="text" value=""/>
</label>
</div>
</div>
<div class="panel-footer text-center">
<button type="submit" class="button"> 保存 </button>
<button type="button" class="button" onclick="javascript:window.history.go(-1);"> 返回 </button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
</section>
</div>
<style>
/* demo page styles */
body { min-height: 2300px; }
.content-header b,
.admin-form .panel.heading-border:before,
.admin-form .panel .heading-border:before {
transition: all 0.7s ease;
}
/* responsive demo styles */
@media (max-width: 800px) {
.admin-form .panel-body { padding: 18px 12px; }
}
</style>
<style>
.ui-datepicker select.ui-datepicker-month,
.ui-datepicker select.ui-datepicker-year {
width: 48%;
margin-top: 0;
margin-bottom: 0;
line-height: 25px;
text-indent: 3px;
color: #888;
border-color: #DDD;
background-color: #FDFDFD;
-webkit-appearance: none; /*Optionally disable dropdown arrow*/
}
</style>
<script src="${pageContext.request.contextPath }/vendor/jquery/jquery-1.11.1.min.js"></script>
<script src="${pageContext.request.contextPath }/vendor/jquery/jquery_ui/jquery-ui.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/jquery.validate.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/additional-methods.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/jquery-ui-datepicker.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/utility/utility.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/demo/demo.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/main.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/pages.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/items.js"></script>
</body>
</html>
<%@ 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>
<html>
<!-- Mirrored from admindesigns.com/demos/absolute/1.1/admin_forms-validation.html by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 06 Aug 2015 02:56:15 GMT -->
<head>
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<title> 油画商城--分类列表 </title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/skin/default_skin/css/theme.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/css/admin-forms.css">
<link rel="shortcut icon" href="${pageContext.request.contextPath}/assets/img/favicon.ico">
<script type="text/javascript">
function saveUI(){
window.location.href="${pageContext.request.contextPath}/CategoryServlet?method=saveUI";
}
</script>
</head>
<body class="admin-validation-page" data-spy="scroll" data-target="#nav-spy" data-offset="200">
<div id="main">
<%@ include file="header.jsp" %>
<%@ include file="left.jsp" %>
<section id="content_wrapper">
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 分类列表 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<div class="panel-menu">
<div class="row">
<div class="hidden-xs hidden-sm col-md-3">
<div class="btn-group">
<button type="button" class="btn btn-default light">
<i class="fa fa-refresh"></i>
</button>
<button type="button" class="btn btn-default light">
<i class="fa fa-trash"></i>
</button>
<button type="button" class="btn btn-default light" onclick=saveUI()>
<i class="fa fa-plus" onclick="javascript:window.location.href='/employee/to_add';"></i>
</button>
</div>
</div>
<div class="col-xs-12 col-md-9 text-right">
<div class="btn-group">
<button type="button" class="btn btn-default light">
<i class="fa fa-chevron-left"></i>
</button>
<button type="button" class="btn btn-default light">
<i class="fa fa-chevron-right"></i>
</button>
</div>
</div>
</div>
</div>
<div class="panel-body pn">
<table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
<thead>
<tr class="">
<th class="hidden-xs">名称</th>
<th class="hidden-xs">描述</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="category" items="${list}">
<tr class="message-unread">
<td>${category.cname}</td>
<td>
${category.cdesc}
</td>
<td>
<a href="${pageContext.request.contextPath }/CategoryServlet?method=edit&cid=${category.cid}">编辑</a>
<a href="#">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
</section>
</div>
<style>
/* demo page styles */
body { min-height: 2300px; }
.content-header b,
.admin-form .panel.heading-border:before,
.admin-form .panel .heading-border:before {
transition: all 0.7s ease;
}
/* responsive demo styles */
@media (max-width: 800px) {
.admin-form .panel-body {padding: 18px 12px;}
}
</style>
<style>
.ui-datepicker select.ui-datepicker-month,
.ui-datepicker select.ui-datepicker-year {
width: 48%;
margin-top: 0;
margin-bottom: 0;
line-height: 25px;
text-indent: 3px;
color: #888;
border-color: #DDD;
background-color: #FDFDFD;
-webkit-appearance: none; /*Optionally disable dropdown arrow*/
}
</style>
<script src="${pageContext.request.contextPath }/vendor/jquery/jquery-1.11.1.min.js"></script>
<script src="${pageContext.request.contextPath }/vendor/jquery/jquery_ui/jquery-ui.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/jquery.validate.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/additional-methods.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/js/jquery-ui-datepicker.min.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/utility/utility.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/demo/demo.js"></script>
<script src="${pageContext.request.contextPath }/assets/js/main.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/pages.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/items.js"></script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<!-- Mirrored from admindesigns.com/demos/absolute/1.1/admin_forms-validation.html by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 06 Aug 2015 02:56:15 GMT -->
<head>
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<title>油画商城--修改分类</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/skin/default_skin/css/theme.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/admin-tools/admin-forms/css/admin-forms.css">
<link rel="shortcut icon" href="${pageContext.request.contextPath }/assets/img/favicon.ico">
</head>
<body class="admin-validation-page" data-spy="scroll" data-target="#nav-spy" data-offset="200">
<div id="main">
<%@ include file="header.jsp" %>
<%@ include file="left.jsp" %>
<section id="content_wrapper">
<section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 修改分类 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<form id="admin-form" name="addForm" action="/department/add" method="post">
<div class="panel-body bg-light">
<div class="section-divider mt20 mb40">
<span> 基本信息 </span>
</div>
<div class="section row">
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
名称
</label>
</label>
</div>
<div class="col-md-6">
<label for="sn" class="field">
<input id="sn" name="cname" class="gui-input" placeholder="名称" type="text" value="${category.cname }"/>
</label>
</div>
</div>
<div class="section row">
<div class="col-md-2"></div>
<div class="col-md-1">
<label for="sn" class="field prepend-icon">
<label for="sn" class="field-icon">
描述
</label>
</label>
</div>
<div class="col-md-6">
<label for="address" class="field">
<input id="address" name="cdesc" class="gui-input" placeholder="描述" type="text" value="${category.cdesc }"/>
</label>
</div>
</div>
<div class="panel-footer text-center">
<button type="submit" class="button"> 保存 </button>
<button type="button" class="button" onclick="javascript:window.history.go(-1);"> 返回 </button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
</section>
</div>
<style>
/* demo page styles */
body { min-height: 2300px; }
.content-header b,
.admin-form .panel.heading-border:before,
.admin-form .panel .heading-border:before {
transition: all 0.7s ease;
}
/* responsive demo styles */
@media (max-width: 800px) {
.admin-form .panel-body { padding: 18px 12px; }
}
</style>
<style>
.ui-datepicker select.ui-datepicker-month,
.ui-datepicker select.ui-datepicker-year {
width: 48%;
margin-top: 0;
margin-bottom: 0;
line-height: 25px;
text-indent: 3px;
color: #888;
border-color: #DDD;
background-color: #FDFDFD;
-webkit-appearance: none; /*Optionally disable dropdown arrow*/
}
</style>
<script src="vendor/jquery/jquery-1.11.1.min.js"></script>
<script src="vendor/jquery/jquery_ui/jquery-ui.min.js"></script>
<script src="assets/admin-tools/admin-forms/js/jquery.validate.min.js"></script>
<script src="assets/admin-tools/admin-forms/js/additional-methods.min.js"></script>
<script src="assets/admin-tools/admin-forms/js/jquery-ui-datepicker.min.js"></script>
<script src="assets/js/utility/utility.js"></script>
<script src="assets/js/demo/demo.js"></script>
<script src="assets/js/main.js"></script>
<script type="text/javascript" src="js/pages.js"></script>
<script type="text/javascript" src="js/items.js"></script>
</body>
</html>
正在回答
同学你好,小慕测试贴出代码中的edit的内容,是可以正确执行的,同学可以尝试从如下方法排查一下
1、修改后查看请求是否正确发送,控制台有没有报错信息
2、在edit方法中添加输出语句,查看是不是执行到了
如果输出语句正确执行完毕,说明修改方法已经正确执行了,此时再来排查是不用响应或调整有问题。
如果输出语句并没有执行,说明并没有正确执行到edit方法中,此时同学需要再来排查是不是参数传递或者比较的代码有问题。
祝:学习愉快~
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星