• spring MVC实现增加与查询-


    1.运行环境  

    idea 2017.3.5  & java 1.9  & Tomcat 8.5.31

    文件布局

    2.实现  在网页上 如图

    <1>登录   添加页面 端口号/add

     <2>添加成功后

    <3>查看添加信息

    <4> 查询更多

     3.运行原理  

    发请求--》web.xml-》控制器UserController(处理服务)-》dispatcher -servlet.xml  转发服务 -》视图 *.jsp-》 显示 

    4.代码详情

    web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
     5          version="4.0">
     6     <context-param>
     7         <param-name>contextConfigLocation</param-name>
     8         <param-value>/WEB-INF/applicationContext.xml</param-value>
     9     </context-param>
    10     <listener>
    11         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    12     </listener>
    13     <servlet>
    14         <servlet-name>dispatcher</servlet-name>
    15         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    16         <load-on-startup>1</load-on-startup>
    17     </servlet>
    18     <servlet-mapping>
    19         <servlet-name>dispatcher</servlet-name>
    20 <!--只能通过后缀名是.mvc 访问   -->
    21         <url-pattern>*.mvc</url-pattern>
    22     </servlet-mapping>
    23 
    24 
    25 </web-app>
    web文件配置

    实体类

     1 #Gender.java
     2 package ke_1014.cn.ccut.domain;
     3 public enum Gender {
     4     MALE,
     5     FEMALE;
     6 }
     7 
     8 
     9 #User.java
    10 package ke_1014.cn.ccut.domain;
    11 public class User {
    12     private String name;
    13     private String lastname;
    14     private String password;
    15     private String detail;
    16     //@DateTimeFormat(pattern="yyyy-MM-dd")
    17     private String birthDate;
    18     private Gender gender;
    19     private String country;
    20     private boolean nonSmoking;
    21 
    22     public String getName() {
    23         return name;
    24     }
    25 
    26     public void setName(String name) {
    27         this.name = name;
    28     }
    29 
    30     public String getLastname() {
    31         return lastname;
    32     }
    33 
    34     public void setLastname(String lastname) {
    35         this.lastname = lastname;
    36     }
    37     
    38     public String getPassword() {
    39         return password;
    40     }
    41 
    42     public void setPassword(String password) {
    43         this.password = password;
    44     }
    45 
    46     public String getDetail() {
    47         return detail;
    48     }
    49 
    50     public void setDetail(String detail) {
    51         this.detail = detail;
    52     }
    53 
    54     public String getBirthDate() {
    55         return birthDate;
    56     }
    57 
    58     public void setBirthDate(String birthDate) {
    59         this.birthDate = birthDate;
    60     }
    61 
    62     public Gender getGender() {
    63         return gender;
    64     }
    65 
    66     public void setGender(Gender gender) {
    67         this.gender = gender;
    68     }
    69 
    70     public String getCountry() {
    71         return country;
    72     }
    73 
    74     public void setCountry(String country) {
    75         this.country = country;
    76     }
    77     public boolean isNonSmoking() {
    78         return nonSmoking;
    79     }
    80     public void setNonSmoking(boolean nonSmoking) {
    81         this.nonSmoking = nonSmoking;
    82     }
    83 }
    对应实体类

    UserController.java

     1 package ke_1014.cn.ccut.controller;
     2 import java.util.HashMap;
     3 import java.util.Map;
     4 import org.springframework.stereotype.Controller;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.servlet.ModelAndView;
     7 import ke_1014.cn.ccut.domain.Gender;
     8 import ke_1014.cn.ccut.domain.User;
     9 @Controller
    10 public class UserController {
    11     private static final String[] countries = { "China","Turkey", "United States", "Germany" };
    12     Map<String,User> mp=new HashMap<String,User>();
    13     //加载页面信息
    14     @RequestMapping(value ="/add")
    15     public ModelAndView add() {
    16         ModelAndView modelAndView = new ModelAndView("useradd", "user", new User());
    17         modelAndView.addObject("genders", Gender.values());
    18         modelAndView.addObject("countries", countries);        
    19         return modelAndView;
    20     }
    21     //查询当前用户信息
    22     @RequestMapping(value = "/selectU")
    23     public ModelAndView processUser(String  name) {
    24         ModelAndView modelAndView = new ModelAndView();
    25         modelAndView.setViewName("selectUser");
    26         modelAndView.addObject("u", mp.get(name));
    27         return modelAndView;
    28     }
    29     //添加一个用户
    30     @RequestMapping(value = "/addok")
    31     public ModelAndView processUser1(User user) {
    32         mp.put(user.getName(), user);
    33         System.out.println(mp.size());
    34         ModelAndView modelAndView = new ModelAndView();
    35         modelAndView.setViewName("addok");
    36         modelAndView.addObject("u", user);    
    37         return modelAndView;
    38     }
    39     //查询添加的所有
    40     @RequestMapping(value = "/selectA")
    41     public ModelAndView processselectA() {
    42         ModelAndView modelAndView =new ModelAndView();
    43         modelAndView.setViewName("selectAll");
    44         modelAndView.addObject("u",mp);
    45         return modelAndView;
    46     }
    47 //查询指定用户
    48     @RequestMapping(value = "/selectB")
    49         public ModelAndView processselectB() {
    50         
    51             ModelAndView modelAndView =new ModelAndView();
    52             modelAndView.setViewName("selectAll");
    53             User use1=mp.get("456");
    54             modelAndView.addObject("u",use1 );
    55             return modelAndView;
    56         }
    57 }
    UserController

    web-infpages

    useradd.jsp

     1 <%@ page contentType="text/html; charset=utf-8" %>
     2 <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc" %>
     3 <html>
     4 <head>
     5     <title>用户添加</title>
     6 </head>
     7 <body><center>
     8 
     9 <h2>用户添加表单</h2>
    10 <mvc:form modelAttribute="user" action="addok.mvc">
    11     <table>
    12         <tr>
    13             <td><mvc:label path="name">用户名:</mvc:label></td>
    14             <td><mvc:input path="name" /></td>
    15         </tr>
    16         <tr>
    17             <td><mvc:label path="lastname"></mvc:label></td>
    18             <td><mvc:input path="lastname" /></td>
    19         </tr>
    20         <tr>
    21             <td><mvc:label path="password">密码</mvc:label></td>
    22             <td><mvc:password path="password" /></td>
    23         </tr>
    24            <tr>
    25             <td><mvc:label path="detail">用户详情</mvc:label></td>
    26             <td><mvc:textarea path="detail" /></td>
    27         </tr>
    28         <tr>
    29             <td><mvc:label path="birthDate">出生日期 </mvc:label></td>
    30             <td><mvc:input path="birthDate" /></td>
    31         </tr>
    32         <tr>
    33             <td><mvc:label path="gender">性别</mvc:label></td>
    34             <td><mvc:radiobuttons path="gender" items="${genders}" /></td>
    35         </tr>
    36         <tr>
    37             <td><mvc:label path="country">城市</mvc:label></td>
    38             <td><mvc:select path="country" items="${countries}" /></td>
    39         </tr>
    40         <tr>
    41             <td><mvc:label path="nonSmoking">吸烟不</mvc:label></td>
    42             <td><mvc:checkbox path="nonSmoking" /></td>
    43         </tr>
    44         <tr>
    45             <td colspan="2">
    46                 <input type="submit" value="Submit" />
    47             </td>
    48         </tr>
    49     </table>  
    50 </mvc:form></center>
    51 </body>
    52 </html>
    用户添加页面

    addok.jsp

    1 <%@ page contentType="text/html; charset=UTF-8" %>
    2 <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc" %>
    3 <html><center>
    4 姓名:${u.name}  添加成功!<br />
    5 <button> <a href="selectU.mvc?name=${u.name}">查看这个姓名的更多信息</a><br /></button>
    6 <button> <a href="selectA.mvc">查看所有已添加用户信息</a></button>
    7  </center>
    8     </body>
    9 </html>
    用户添加成功页面

    selectUser.jsp

     1 <%@ page contentType="text/html; charset=UTF-8" %>
     2 <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc" %>
     3 <html>
     4 <head><center>
     5     <title>成功添加</title>
     6 </head>
     7 <body>
     8     <h2>用户添加结果</h2>
     9     <table>
    10         <tr>
    11             <td>姓名</td>
    12             <td>${u.name}</td>
    13         </tr>
    14         <tr>
    15             <td></td>
    16             <td>${u.lastname}</td>
    17         </tr>
    18         <tr>
    19             <td>用户密码</td>
    20             <td>${u.password}</td>
    21         </tr>
    22         <tr>
    23             <td>用户详情</td>
    24             <td>${u.detail}</td>
    25         </tr>
    26         <tr>
    27             <td>出生日期</td>
    28             <td>${u.birthDate}</td>
    29         </tr>
    30         <tr>
    31             <td>性别</td>
    32             <td>${u.gender}</td>
    33         </tr>
    34         <tr>
    35             <td>城市</td>
    36             <td>${u.country}</td>
    37         </tr>
    38         <tr>
    39             <td>不吸烟</td>
    40             <td>${u.nonSmoking}</td>
    41         </tr>
    42     </table>
    43     <button><a href="add.mvc">继续添加</a> </button>
    44     <button> <a href="selectA.mvc">查看所有已添加用户信息</a></button>
    45 
    46 </body>
    47 </body>
    48 </html>
    查询当前用户

    selectAll.jsp

     1 <%@ page contentType="text/html; charset=UTF-8" %>
     2 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
     3 <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc"   %>
     4 <html><center>
     5 <head>
     6     <title>成功添加</title>
     7 </head>
     8 <body>
     9     <h2>用户添加结果</h2>
    10     <table  border="1" >
    11         <tr>
    12             <th>姓名</th>
    13             <th>用户密码</th>
    14             <th>城市</th>
    15             <%--<c:forEach var="item" items=" ${u.keySet()}">--%>
    16         </tr>
    17             <c:forEach var="item" items="${u}">
    18             <tr>
    19                 <%--<td>${item.key}</td>--%>
    20                 <td>${item.value.name} </td>
    21                 <td>${item.value.password}</td>
    22                     <td>${item.value.country}</td>
    23            </tr>
    24             </c:forEach>
    25     </table><br />
    26     <button><a href="add.mvc">继续添加</a> </button>
    27 </body>
    28     </center>
    29 </html>
    查询添加所有用户信息
  • 相关阅读:
    信用卡知识:自动扣款日不等于最后还款日_刷卡技巧_财经_腾讯网
    北京,大兴,采育,京福路边上。嘿嘿,交通还是很便利的。
    想知道:北京市 亦庄交通队在哪?
    bda_百度百科
    聚合服务资费标准
    公租自行车-北京经济技术开发区
    北京公共自行车-北京市交通委员会运输管理局
    北京公共自行车租赁方法_百度知道
    NEXT | 不错过任何一个新产品
    吸血鬼日记 第五季 16 | 美剧单词
  • 原文地址:https://www.cnblogs.com/zhenqk/p/11675214.html
Copyright © 2020-2023  润新知