SpringMVC控制器03

一、SpringMVC对象属性自动封装

接着02的内容

1e4832c5a39aff7eb66f30241a15b78077a.jpg

add.java

<%@ 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}/student/save.do" 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 type="submit" value="提交"/>
		</td>
	</tr>
</table>
</form>
</body>
</html>

StudentController.java

package com.java.controller;

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.java.model.Student;

//Controller说明是控制器
//分模块,请求的时候要加student在前面
@Controller
@RequestMapping("/student")

public class StudentController {
	//模拟数据库数据
	private static List<Student> studentList=new ArrayList<Student>();
	static{
		studentList.add(new Student(1,"张三",11));
		studentList.add(new Student(2,"李四",12));
		studentList.add(new Student(3,"王五",13));
	}
	
	
	//ModelAndView:返回模型和视图
	//模型:数据的封装,试图:返回到哪个jsp
	//请求的时候:student.list
	@RequestMapping("/list")
	private ModelAndView list(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("studentList", studentList);//把数据封装起来
		mv.setViewName("student/list");//返回到哪个jsp
		return mv;
		
	}
	//RequestParam这样写就可以把学生id的值自动注入到String id
	
	//因为添加和修改都是用preSave这个方法,但是添加是没有id传过来的
	//required=false意思是,这个id不用也可以,用也可以,这样不用的时候就添加,用的时候就修改
	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
		ModelAndView mav=new ModelAndView();
		//如果id=null就是跳到添加页面,不为空就获取该条数据返回到修改页面,id-1=索引。索引从零开始
		if(id!=null){
			mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
			mav.setViewName("student/update");
		}else{
			mav.setViewName("student/add");			
		}
		return mav;
	}
	

	//数据自动封装成student对象
	//在添加学生页面下面两条数据
	//<input type="text" name="name"/>对应model类的学生name
	//<input type="text" name="age"/>对应model类的学生age
	//这样数据就能自动封装成对象
	@RequestMapping("/save")
	public String save(Student student){
		//如果id不等于零就只执行更新
	if(student.getId()!=0){
		Student s=studentList.get(student.getId()-1);
		s.setName(student.getName());
		s.setAge(student.getAge());
	}else{
		//否则执行添加
		studentList.add(student);			
	}
	return "redirect:/student/list.do";
	
}
	
	
}

结果

b22d98810f703bd343cb4c5838c35de728c.jpg

然后调试发现我们输入的数据自动封装成student对象

7228d565a9353f42f97896ac06d22d9ebe3.jpg

但是我们发现存入的数据是乱码,这是因为spring对中文还不支持,所以要在web.xml配置过滤器

7c236944049935609510c9da598772f2505.jpg

web.xml

<?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>SpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
 
  </welcome-file-list>
  
  <!-- 拦截请求交给Spring处理 -->
  <!-- DispatcherServlet负责请求的分发 -->
  <!-- 拦截所有的.do方法,可以自定义 -->
   <servlet>
		<servlet-name>springmvc</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>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<!-- 过滤器,解决中文乱码问题,所以以后开发都要配置CharacterEncodingFilter -->
	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	
</web-app>

结果解决了对象属性自动封装

196aa6eddf93fa25a925384b0832ab7e9dd.jpg

模拟修改(没有数据库,只是模拟,是有问题的)年龄改为22

387023a17c5d3e86e19899f0a428c6d27cf.jpg

9852a74b59a660f106617b18bc4b54e6f83.jpg

最后模拟删除

list.java

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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}/student/preSave.do">添加学生</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}/student/preSave.do?id=${student.id}">修改</a>
		&nbsp;&nbsp;
			<a href="${pageContext.request.contextPath}/student/delete.do?id=${student.id}">删除</a>
		</td>
	</tr>
	</c:forEach>
</table>
</body>
</html>

StudentController.java

package com.java.controller;

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.java.model.Student;

//Controller说明是控制器
//分模块,请求的时候要加student在前面
@Controller
@RequestMapping("/student")

public class StudentController {
	//模拟数据库数据
	private static List<Student> studentList=new ArrayList<Student>();
	static{
		studentList.add(new Student(1,"张三",11));
		studentList.add(new Student(2,"李四",12));
		studentList.add(new Student(3,"王五",13));
	}
	
	
	//ModelAndView:返回模型和视图
	//模型:数据的封装,试图:返回到哪个jsp
	//请求的时候:student.list
	@RequestMapping("/list")
	private ModelAndView list(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("studentList", studentList);//把数据封装起来
		mv.setViewName("student/list");//返回到哪个jsp
		return mv;
		
	}
	//RequestParam这样写就可以把学生id的值自动注入到String id
	
	//因为添加和修改都是用preSave这个方法,但是添加是没有id传过来的
	//required=false意思是,这个id不用也可以,用也可以,这样不用的时候就添加,用的时候就修改
	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
		ModelAndView mav=new ModelAndView();
		//如果id=null就是跳到添加页面,不为空就获取该条数据返回到修改页面,id-1=索引。索引从零开始
		if(id!=null){
			mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
			mav.setViewName("student/update");
		}else{
			mav.setViewName("student/add");			
		}
		return mav;
	}
	
	//数据自动封装成student对象
	//在添加学生页面下面两条数据
	//<input type="text" name="name"/>对应model类的学生name
	//<input type="text" name="age"/>对应model类的学生age
	//这样数据就能自动封装成对象
	@RequestMapping("/save")
	public String save(Student student){
		//如果id不等于零就只执行更新
	if(student.getId()!=0){
		Student s=studentList.get(student.getId()-1);
		s.setName(student.getName());
		s.setAge(student.getAge());
	}else{
		//否则执行添加
		studentList.add(student);			
	}
           //重定向
	return "redirect:/student/list.do";
           //转发
	//return "forward:/student/list.do";
}
        //删除需要id,所以要注入@RequestParam("id")给int id
	@RequestMapping("/delete")
	public String delete(@RequestParam("id") int id){
		studentList.remove(id-1);
		return "redirect:/student/list.do";
	}
	
}

7d027978377519339a93c5a1b09320db3b1.jpg

 

8bb412b09d55078bcfee51c6ab796326c56.jpg

转载于:https://my.oschina.net/u/3848699/blog/2246193

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值