首页 > 代码库 > springmvc restful风格操作
springmvc restful风格操作
ssm框架
controller:
package com.sgcc.controller; import java.util.ArrayList; import java.util.List; import org.omg.CORBA.PUBLIC_MEMBER; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import com.sgcc.entity.User; @Controller @RequestMapping("/user")//窄化--分模块开发,团队协作-- 注意页面跳转 加/ 或视图解析器 public class UserController { private List<User> list = new ArrayList<User>(); public UserController() { list.add(new User(1,"张三","1111")); list.add(new User(2,"李四","1111")); list.add(new User(3,"王五","3333")); } //查询所有用户 @RequestMapping("/list") public String list(ModelMap map){ map.addAttribute("list", list); //System.out.println(list.toString()); return "/list.jsp"; } //添加用户 @RequestMapping("/add") public String add(User user){ user.setId(list.get(list.size()-1).getId()+1); list.add(user); //return "/user/list.do" return "redirect:list.do"; } //删除用户 @RequestMapping("/delete") public String delte(int id){ for (int i = 0; i < list.size(); i++) { if (list.get(i).getId()==id) { list.remove(i); break; } } return "redirect:list.do"; } //传统url delete?id=123; //restful风格的开发 delete/123 //url /user/123/delete1 @RequestMapping("/{id}/delete1") public String delete1(@PathVariable int id){ for (int i = 0; i < list.size(); i++) { if (list.get(i).getId()==id) { list.remove(i); break; } } return "redirect:/user/list.do"; } }
前端:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% 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> <base href="http://www.mamicode.com/"> <title>My JSP ‘index.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css"> --> </head> <body> <table width="80%" align="center"> <tr> <td colspan="4" align="right"><a href="http://www.mamicode.com/add.jsp">添加</a></td> </tr> <tr> <td>编号</td> <td>姓名</td> <td>密码</td> <td>操作</td> </tr> <c:forEach items = "${list }" var="bean"> <tr> <td>${bean.id }</td> <td>${bean.name }</td> <td>${bean.pwd }</td> <td><a href="http://www.mamicode.com/user/delete.do?id=${bean.id }">删除</a> <td><a href="http://www.mamicode.com/user/${bean.id }/delete1.do">删除2</a> </td> </tr> </c:forEach> </table> </body> </html>
springmvc restful风格操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。