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
"/user")//窄化--分模块开发,团队协作-- 注意页面跳转 加/ 或视图解析器
@RequestMapping(
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 < 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 < 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%>"><title>My JSP 'index.jsp' starting page</title></br> <meta http-equiv="pragma" content="no-cache"></br> <meta http-equiv="cache-control" content="no-cache"></br> <meta http-equiv="expires" content="0"> </br> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"></br> <meta http-equiv="description" content="This is my page"></br> <!--</br> <link rel="stylesheet" type="text/css" href="styles.css"></br> --></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>