码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc restful风格操作

时间:2016-11-19 23:29:29      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:base   gets   lte   ++   name   bean   turn   return   styles   

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="<%=basePath%>">
    
    <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="styles.css">
    -->
  </head>
  
  <body>
       <table width="80%" align="center">
           <tr>
               <td colspan="4" align="right"><a href="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="user/delete.do?id=${bean.id }">删除</a>
               <td><a href="user/${bean.id }/delete1.do">删除2</a>
               </td>
           </tr>
           </c:forEach>
       </table>
  </body>
</html>

 

springmvc restful风格操作

标签:base   gets   lte   ++   name   bean   turn   return   styles   

原文地址:http://www.cnblogs.com/alloevil/p/6081784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!