整套ssm框架工程+sql表下载网址:
http://download.youkuaiyun.com/detail/sinat_33949861/9729446【注意哦,这项目非免费】
StuController.java
package com.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.demo.pojo.Student;
import com.demo.service.StuService;
@Controller
@RequestMapping("/stu")
public class StuController {
@Autowired
private StuService stuService;
@RequestMapping("/stuSave")
public String stuSave(Student stu) {
boolean b = stuService.addStu(stu);
if (b) {
return "redirect:/stu/findAll.do";
} else {
return "index";
}
}
@RequestMapping("/updateSave")
public String updateSave(Student stu) {
boolean b = stuService.updateStu(stu);
System.out.println(b);
if (b) {
return "redirect:/stu/findAll.do";
} else {
return "index";
}
}
@RequestMapping("/findById")
public ModelAndView findById(@RequestParam("id") int id) {
ModelAndView ma = new ModelAndView();
Student s = stuService.findById(id);
ma.addObject("stu", s);
ma.setViewName("updateStu");
return ma;
}
@RequestMapping("/delStu")
public String delStu(@RequestParam("id") int id) {
boolean b = stuService.deleteStu(id);
if (b) {
return "redirect:/stu/findAll.do";
} else {
return "index";
}
}
@RequestMapping("/findAll")
public ModelAndView findAll() {
ModelAndView mo = new ModelAndView();
List<Student> list = stuService.findAll();
mo.addObject("list", list);
mo.setViewName("list");
return mo;
}
}
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>addStu.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="stu/updateSave.do" method="post">
<table align="center" border="1" cellpadding="0" cellspacing="0">
<%-- <tr>
<th>id</th><th><input type="text" readonly="readonly" name="id" value="${stu.id }"/></th>
</tr> --%>
<tr>
<th>name</th><th><input type="text" name="tname" value="${stu.tname }" />
</tr>
<tr>
<th>tpwd</th><th><input type="text" name="tpwd" value="${stu.tpwd }"/>
</tr>
<tr>
<th>age</th><th><input type="text" name="age" value="${stu.age }"/>
</tr>
<tr>
<th>sex</th>
<th><input type="radio" name="sex" value="1" ${stu.sex==1?"checked='checked'":"" } />男
<input type="radio" name="sex" value="0" ${stu.sex==0?"checked='checked'":"" }/>女</th>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
错误页面:
解决错误的方法:
在修改页面将添加到修改页面
<tr>
<th>id</th><th><input type="text" readonly="readonly" name="id" value="${stu.id }"/></th>
</tr>