SpringMVC学习三

基于SpringMVC实现简单用户管理

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
  
  
  <!-- 
          配置org.springframework.web.filer.HiddenHttpMethodFilter:可以把POST请求转为DELETE或POST请求
   -->
   <filter>
             <filter-name>HiddenHttpMethodFilter</filter-name>
               <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
   
   </filter>
   <filter-mapping>
               <filter-name>HiddenHttpMethodFilter</filter-name>
               <!-- 过滤所有请求 -->
               <url-pattern>/*</url-pattern>
   </filter-mapping>
   <!-- 配置DispatcherServlet -->
  
          <servlet>
              <servlet-name>springDispatcherServlet</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <!-- 配置 DispatcherServlet的一个初始化参数:配置SpringMvc 配置文件的位置和名称-->
                   <init-param>
                      <param-name>contextConfigLocation</param-name>
                      <param-value>classpath:springmvc.xml</param-value>
              </init-param>
               <init-param>
                      <param-name>encoding</param-name>
                      <param-value>UTF-8</param-value>
              </init-param>
              <!-- 也可以不通过contextConfigLocation 来配置SpringMvc的配置文件,而使用默认的
                      默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml
                          /WEB-INF/springDispatcherServlet-servlet.xml
              -->
                  
          <!-- 设置启动 -->
          <load-on-startup>1</load-on-startup>
          </servlet>
          
          <servlet-mapping>
          <!-- 请求处理 -->
              <servlet-name>springDispatcherServlet</servlet-name>
              <!-- /:应答所有请求 -->
              <url-pattern>/</url-pattern>
          </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 
         xmlns:aop="http://www.springframework.org/schema/aop"
 
       xmlns:mvc="http://www.springframework.org/schema/mvc"
 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
       xmlns:context="http://www.springframework.org/schema/context" 
 
       xmlns:p="http://www.springframework.org/schema/p"
 
  xsi:schemaLocation="
 
              http://www.springframework.org/schema/beans    
 
                 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     
              http://www.springframework.org/schema/mvc      
 
              http://www.springframework.org/schema/tx/spring-mvc-4.0.xsd
 
               http://www.springframework.org/schema/context
 
               http://www.springframework.org/schema/context/spring-context-4.0.xsd
 
               http://www.springframework.org/schema/aop
 
             http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
             
             <!-- 配置自定义扫描包 -->
             <context:component-scan base-package="como.springmvc.handlers"></context:component-scan>
           <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 -->
           
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <!-- spring中加入jstl标签库 -->
           <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
                       <!-- 前缀 -->
                       <property name="prefix" value="/WEB-INF/views/"></property>
                       <!-- 后缀 -->
                       <property name="suffix" value=".jsp"></property>
           </bean>
           </beans>
 

User.java

package com.springmvc.entity;

public class User {

    
                private String username;
                private String password;
                private String email;
                private int age;
                
                
                public User(String username,String password,String email,int age) {
                    this.username=username;
                    this.password=password;
                    this.email=email;
                    this.age=age;
                }
                
                public String getUsername() {
                    return username;
                }
                public void setUsername(String username) {
                    this.username = username;
                }
                public String getPassword() {
                    return password;
                }
                public void setPassword(String password) {
                    this.password = password;
                }
                public String getEmail() {
                    return email;
                }
                public void setEmail(String email) {
                    this.email = email;
                }
                public int getAge() {
                    return age;
                }
                public void setAge(int age) {
                    this.age = age;
                }
                
                
                @Override
                public String toString() {
                    return "User [username=" + username + ", password="
                            + password + ", email=" + email + ", age=" + age
                            +  "]";
                }
                
                
                
                
}

UserController.java

package com.springmvc.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.springmvc.entity.User;


@Controller
@RequestMapping("/user")
public class UserController {
    private Map<String,User> users = new HashMap<String,User>();
    
    //创建构造函数
    public UserController(){
        //给users里添加属性
        users.put("sddy", new User("sddy","123","123@123",15));
        users.put("sdy", new User("sdy","1234","123@12",15));
        users.put("ddy", new User("ddy","23","123@123",15));
        users.put("sdd", new User("sddy","12","123@123",15));
    }
    @RequestMapping(value="/users",method=RequestMethod.GET)
    public String List(Model model){ 
        model.addAttribute("users",users);//传值给jsp页面,第一个users为jsp中items的users
        return "user/list";
        
    }
    //链接到add页面是get请求,访问此代码
    @RequestMapping(value="/add",method=RequestMethod.GET)
    public String add(Model model){ 
        model.addAttribute(new User());
        //服务器端跳转forword
        return "user/add";
        
    }
    //重载,在添加用户时,是post请求,访问此代码
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(User user){ 
        users.put(user.getUsername(), user);
        //客户端跳转
        return "redirect:/user/users";
        
    }

}

list.jsp

<%@ 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>用户列表</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>
  <a href="add">添加</a>
       <c:foreach items="${users}" var="um">
       ${um.value.username}
       ---${um.value.password}
       ---${um.value.email}
       ---${um.value.age}
       
       
       </c:foreach>

  </body>
</html>

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%
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 'add.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>
  <!-- 自动把表单的值添加到user中 -->
   <sf:form method="post" modelAttribute="user">
           Username:<sf:input path="usename"/><br>
           Password:<sf:password path="password"/><br>
           Email:<sf:input path="email"/><br>
           Age:<sf:input path="age"/><br>
           <input type="submit" value="add"/>
           
   
   
   
   
   
   </sf:form>
  </body>
</html>

 

posted @ 2017-12-19 10:28  蓉啊  阅读(170)  评论(0编辑  收藏  举报