• 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>();

    </span><span style="color: #0000ff">public</span><span style="color: #000000"> UserController() {</br>
        list.add(</span><span style="color: #0000ff">new</span> User(1,"张三","1111"<span style="color: #000000">));</br>
        list.add(</span><span style="color: #0000ff">new</span> User(2,"李四","1111"<span style="color: #000000">));</br>
        list.add(</span><span style="color: #0000ff">new</span> User(3,"王五","3333"<span style="color: #000000">));</br>
        
    }</br>
    </span><span style="color: #008000">//</span><span style="color: #008000">查询所有用户</span></br>
    @RequestMapping("/list"<span style="color: #000000">)</br>
    </span><span style="color: #0000ff">public</span><span style="color: #000000"> String list(ModelMap map){</br>
        map.addAttribute(</span>"list"<span style="color: #000000">, list);</br>
        </span><span style="color: #008000">//</span><span style="color: #008000">System.out.println(list.toString());</span></br>
        <span style="color: #0000ff">return</span> "/list.jsp"<span style="color: #000000">;</br>
    }</br>
    </span><span style="color: #008000">//</span><span style="color: #008000">添加用户</span></br>
    @RequestMapping("/add"<span style="color: #000000">)</br>
    </span><span style="color: #0000ff">public</span><span style="color: #000000"> String add(User user){</br>
        user.setId(list.get(list.size()</span>-1).getId()+1<span style="color: #000000">);</br>
        list.add(user);</br>
        </span><span style="color: #008000">//</span><span style="color: #008000">return "/user/list.do"</span></br>
        <span style="color: #0000ff">return</span> "redirect:list.do"<span style="color: #000000">;</br>
    }</br>
    </span><span style="color: #008000">//</span><span style="color: #008000">删除用户</span></br>
    @RequestMapping("/delete"<span style="color: #000000">)</br>
    </span><span style="color: #0000ff">public</span> String delte(<span style="color: #0000ff">int</span><span style="color: #000000"> id){</br>
        </span><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; list.size(); i++<span style="color: #000000">) {</br>
            </span><span style="color: #0000ff">if</span> (list.get(i).getId()==<span style="color: #000000">id) {</br>
                list.remove(i);</br>
                </span><span style="color: #0000ff">break</span><span style="color: #000000">;</br>
            }</br>
        }</br>
        </span><span style="color: #0000ff">return</span> "redirect:list.do"<span style="color: #000000">;</br>
    }</br>
    </span><span style="color: #008000">//</span><span style="color: #008000">传统url delete?id=123;</br>
    </span><span style="color: #008000">//</span><span style="color: #008000">restful风格的开发 delete/123</br>
    </span><span style="color: #008000">//</span><span style="color: #008000">url /user/123/delete1</span></br>
    @RequestMapping("/{id}/delete1"<span style="color: #000000">)</br>
    </span><span style="color: #0000ff">public</span> String delete1(@PathVariable <span style="color: #0000ff">int</span><span style="color: #000000"> id){</br>
        </span><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; list.size(); i++<span style="color: #000000">) {</br>
            </span><span style="color: #0000ff">if</span> (list.get(i).getId()==<span style="color: #000000">id) {</br>
                list.remove(i);</br>
                </span><span style="color: #0000ff">break</span><span style="color: #000000">;</br>
            }</br>
        }</br>
        </span><span style="color: #0000ff">return</span> "redirect:/user/list.do"<span style="color: #000000">;</br>
    }</br>
    

    }

    复制代码

    前端:

    复制代码
    <%@ 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%>">

    &lt;title&gt;My JSP 'index.jsp' starting page&lt;/title&gt;</br>
    &lt;meta http-equiv="pragma" content="no-cache"&gt;</br>
    &lt;meta http-equiv="cache-control" content="no-cache"&gt;</br>
    &lt;meta http-equiv="expires" content="0"&gt;    </br>
    &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;</br>
    &lt;meta http-equiv="description" content="This is my page"&gt;</br>
    &lt;!--</br>
    &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;</br>
    --&gt;</br>
    

    </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>

    复制代码
  • 相关阅读:
    bzoj1037: [ZJOI2008]生日聚会Party(DP)
    bzoj1034: [ZJOI2008]泡泡堂BNB(贪心)
    bzoj1025: [SCOI2009]游戏(数学 + 思维 + DP)
    第十章、嵌入式Linux的调试技术 读书笔记
    第九章、硬件抽象层:HAL 读书笔记
    第八章 让开发板发出声音:蜂鸣器驱动
    ANDROID深度探索(卷1)HAL与驱动开发 第七章
    ANDROID深度探索(卷1)HAL与驱动开发 第六章
    ANDROID深度探索(卷1)HAL与驱动开发 第五章
    ANDROID深度探索(卷1)HAL与驱动开发 第四章
  • 原文地址:https://www.cnblogs.com/jpfss/p/8515987.html
Copyright © 2020-2023  润新知