springmvc restful风格操作

2018/03/06 18:16
阅读数 13

<div id="cnblogs_post_body" class="blogpost-body"><p>ssm框架</p> <p>controller:</p> <div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="//common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></span></div> <pre><span style="color: #0000ff">package</span><span style="color: #000000"> com.sgcc.controller;

</span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.ArrayList;</br> </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.List;</br></br>

</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.omg.CORBA.PUBLIC_MEMBER;</br> </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.stereotype.Controller;</br> </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.ui.ModelMap;</br> </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.bind.annotation.PathVariable;</br> </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.bind.annotation.RequestMapping;</br></br>

</span><span style="color: #0000ff">import</span><span style="color: #000000"> com.sgcc.entity.User;</br></br>

@Controller</br> @RequestMapping(</span>"/user")<span style="color: #008000">//</span><span style="color: #008000">窄化--分模块开发,团队协作-- 注意页面跳转 加/ 或视图解析器</span></br> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> UserController {</br> </span><span style="color: #0000ff">private</span> List<User> list = <span style="color: #0000ff">new</span> ArrayList<User><span style="color: #000000">();</br></br>

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

}</span></pre>

<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="//common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></span></div></div> <p>&nbsp;</p> <p>前端:</p> <div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="//common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></span></div> <pre>&lt;%@ page language="java" <span style="color: #0000ff">import</span>="java.util.*" pageEncoding="UTF-8"%&gt;</br> &lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;</br> &lt;%<span style="color: #000000"></br> String path </span>=<span style="color: #000000"> request.getContextPath();</br> String basePath </span>= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"<span style="color: #000000">;</br> </span>%&gt;</br></br></br>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"></br></br> <html></br> <head></br> <base href="<%=basePath%>"></br></br>

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

<body></br> <table width="80%" align="center"></br> <tr></br> <td colspan="4" align="right"><a href="add.jsp">添加</a></td></br> </tr></br> <tr></br> <td>编号</td></br> <td>姓名</td></br> <td>密码</td></br> <td>操作</td></br> </tr></br> <c:forEach items = "${list }" var="bean"></br> <tr></br> <td>${bean.id }</td></br> <td>${bean.name }</td></br> <td>${bean.pwd }</td></br> <td><a href="user/delete.do?id=${bean.id }">删除</a></br> <td><a href="user/${bean.id }/delete1.do">删除2</a></br> </td></br> </tr></br> </c:forEach></br> </table></br> </body></br> </html></pre>

<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="//common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></span></div></div> <p>&nbsp;</p></div>

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部