springmvc复习笔记----springmvc姓名年龄例子:RequestParam 试水

 

继续

继上节http://www.cnblogs.com/tk55/p/6652394.html

 重要部分颜色突出


结构



 

web.xml

乱码处理方面设置 <url-pattern>*</url-pattern>对所有对象起作用

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springmvc01</display-name>
  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <filter>
    <filter-name>characterEncodingfilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
  <filter-name>characterEncodingfilter</filter-name>
  <url-pattern>*</url-pattern>
  </filter-mapping>
</web-app>

 

 

 

 


 

spring-mvc.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--   自动扫描加载注解的包 -->
<context:component-scan base-package="com.ij34.bean"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp" ></property>
</bean>
</beans>

 


index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% response.sendRedirect("students/list"); %>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/students/preSave">添加学生</a>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${studentlist}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td><a href="${pageContext.request.contextPath }/students/preSave?id=${student.id}">修改</a>
&nbsp;
<a href="${pageContext.request.contextPath }/students/delete?id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/students/save"method="post">
<table>
<tr>
<th colspan="2">学生添加</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td colspan="2"><input value="提交" type="submit"/></td>
</tr>
</table>
</form>
</body>
</html>

update.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/students/save"method="post">
<table>
<tr>
<th colspan="2">学生修改</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="${students.name }"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="${students.age }"/></td>
</tr>
<tr>
<td><input type="hidden" name="id" value="${students.id }"/> </td>
<td colspan="2"><input value="提交" type="submit"/></td>
</tr>
</table>
</form>
</body>
</html>

Students.java

package com.ij34.model;

public class Students {
   private int id;
   private String name;
   private int age;
   public Students() {
    // TODO Auto-generated constructor stub   
}
   public Students(int id,String name,int age) {
    this.id=id;
    this.name=name;
    this.age=age;
    
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
   
}

Testhello.java

package com.ij34.bean;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ij34.model.Students;

@Controller
@RequestMapping("/students")  //外部请求的 包括update.jsp里${students.name }
public class Testhello {
  private static List<Students> studentlist=new ArrayList<Students>();
  static{
      studentlist.add(new Students(1, "林肯", 39));
      studentlist.add(new Students(2, "奥巴马", 42));
      studentlist.add(new Students(3, "川普", 66));
      studentlist.add(new Students(4, "布什", 56));
  }
  @RequestMapping("/list")   //@RequestMapping  请求映射
  public ModelAndView list() {
    ModelAndView mav=new ModelAndView();  //ModelAndView  返回模型和视图
    mav.addObject("studentlist", studentlist);
    mav.setViewName("students/list");
   return mav;
 }
  @RequestMapping("/preSave") 
  public ModelAndView preSave(@RequestParam(value="id",required=false)String id ){//@RequestParam  请求参数
    ModelAndView mav=new ModelAndView();
    if(id!=null){
        mav.addObject("students", studentlist.get(Integer.parseInt(id)-1));
        mav.setViewName("students/update");
    }else {
        mav.setViewName("students/add");
    }
    return mav;
      
  }
  
  @RequestMapping("/save") 
  public String save(Students student){
      if(student.getId()!=0){
         Students s=studentlist.get(student.getId()-1);
         s.setName(student.getName());
         s.setAge(student.getAge());
      }else{
          studentlist.add(student);
      }
      return "redirect:/students/list";
  }
  @RequestMapping("/delete") 
  public String delete(@RequestParam("id")int id){//@RequestParam  请求参数
       studentlist.remove(id-1);
      return "redirect:/students/list";  //重定向redirect或者forward转发
  }
}

 

posted on 2017-04-01 00:58  Honey_Badger  阅读(475)  评论(0编辑  收藏  举报

导航

github