随笔 for the first time
SSM
ssm:SpringMVC+Spring+MyBatis
redirect
redirect重定向,在本类的不同方法中跳转。
forward转发,不用类中不同方法的跳转
//跳转到的是jsp页面,".do"是配置的后缀,可改
public class RedirectController{
@RequestMapping("list.do")
public String list(){
return "list";
}
//重定向
@RequestMaooing("add.do")
public String add(){
return "redirect:list.do"
}
}
public class forWordController{
//转发
@RequestMapping("look.do")
public String look{
return "forward:add.do";
}
}
@RequestParam与Model
@RequestParam可以使得前端命名的name和后端接收的name不同名。
model可以把参数存储在里面并传到前端的jsp页面,在jsp页面用${name}获取到传递的name值。
<form action="add.do">
<input type="text" name="username">
</form>
@RequestMapping("add.do")
public String add(@RequestParam("username") String name,Model model){
model.addAttribute("name",name);
return "add";
}
存储一些student数据在List,并传递到前端。<:c >标签引用到的是<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>用的话需要加入依赖。
public class StudentController{
private List<Student> students=new ArrayList<Student>();
@RequestMapping("add.do")
public String add(Student student){
//每次增加学生的时候,students增加一个学生
students.add(student);
return "redirect:list.do";
}
@RequestMapping("list.do")
public String list(Model model){
model.addAttribute("students",students);
return "liststudents";
}
}
<table>
<thread>
<tr>
<td>name</td>
<td>password</td>
</tr>
</thread>
<tbody>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.name}</td>
<td>${student.password}</td>
</tr>
</tbody>
</table>