springmvc--注解crud

来源:互联网 发布:mysql如何登陆 编辑:程序博客网 时间:2024/06/09 15:26
String getList: 获取列表数据 -- 转到列表页面String toAddUI:直接转到添加页面String toUpdateUI:根据id获取对象数据 -- 转到编辑页面deleteById:直接删除 -- 重定向到list方法-列表页getById:toUpdateUI的时候需要根据id获取对象saveOrUpdate:更新之后 -- 重定向到list方法-列表页 @Controller@RequestMapping("/person")public class PersonController {@Resourceprivate PersonService ps; }@Servicepublic class PersonService {private static Map<Integer,Person> pMap = new HashMap<Integer, Person>();private static Integer id = 1;static{Person p = null;for (int i = 0; i < 10; i++) {p = new Person();p.setId(id);p.setAge(21+i);p.setName("jack"+i);pMap.put(id, p);id++;} }// 列表  public List<Person> getAllPerson() {return new ArrayList<Person>(pMap.values());}// 获取一个对象public Person getPersonById(Integer id) {return pMap.get(id);}// 保存public void save(Person p) {if(p.getId() == null){// add id==nullid++;p.setId(id);}pMap.put(p.getId(), p);}public void deleteById(Integer id2) {pMap.remove(id2);}}
以下都是PersonController和相对应的jsp代码----- 列表 -----/**如果参数是Map<String,Object> model,model.put("personList2", personList2); * 在jsp页面<c:forEach items="${personList2 }" var="p"> * items中和put的String值一样就行 * @param model * @return */@RequestMapping("/list")public String list(Map<String,Object> model){List<Person> personList2 = ps.getAllPerson();model.put("personList2", personList2);return "person/list";}/**如果参数是Model model,model.addAttribute(p); * <c:forEach items="${personList }" var="p"> * 无论model.addAttribute(Object);Object是什么值,在items中都是POJO + List(personList,person小写,大写不行) * @param model * @return */@RequestMapping("/list2")public String list(Model model){List<Person> p = ps.getAllPerson();model.addAttribute(p);return "person/list";}    <h2>用户列表</h2>   <a href="${pageContext.request.contextPath}/person/toAddUI.action">new</a>   <table width=60% border=1>   <tr>   <th>id</th>   <th>name</th>   <th>age</th>   <th>op1</th>   <th>op22</th>   </tr>    <c:forEach items="${personList }" var="p">   <tr>   <td>${p.id}</td>   <td>${p.name }</td>   <td>${p.age }</td>   <td><a href="${pageContext.request.contextPath}/person/toUpdateUI2.action?id=${p.id }">修改</a></td>   <td><a href="${pageContext.request.contextPath}/person/delete.action?delId=${p.id }">删除</a></td>   </tr>    </c:forEach>   </table>




----- 编辑1  使用Model传入到spring标签中 -----
/**修改:需要根据id获取修改对象,然后将对象传到页面 * @param id * @param model * @return */@RequestMapping("/toUpdateUI")public String toUpdateUI(Integer id,Model model){Person p = ps.getPersonById(id);model.addAttribute(p);return "person/updateUI";}
-- jsp -- <%@taglib prefix="sf" uri="http://www.springframework.org/tags/form"%><pre name="code" class="java"><body><h2>用户修改</h2><sf:form action="${pageContext.request.contextPath }/person/save.action" method="post" modelAttribute="person"><table width=60px border=1><tr><td>id</td><td> <sf:input path="id"/> </td></tr><tr><td>name</td><td> <sf:input path="name"/> </td></tr><tr><td>age</td><td> <sf:input path="age"/> </td></tr><tr> <td colspan="2"><input type="submit"/></td></tr></table></sf:form></body>

------ 编辑2 使用request传值,在jsp中使用el不使用spring标签库------
----- save ------ 

----- delete ------ 

@RequestMapping("/toUpdateUI2")public String toUpdateUI(Integer id,HttpServletRequest request){Person p = ps.getPersonById(id);request.setAttribute("p", p);return "person/updateUI2";}
0 0
原创粉丝点击