springmvc知识点概况(二)

一、基于注解版本的springmvc小案例

1、首先创建web项目,导入需要的jar包

2、配置web.xml文件

<servlet>
	<servlet-name>DispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml</param-value>
	</init-param>
</servlet>
<servlet-mapping>
	<servlet-name>DispatcherServlet</servlet-name>
	<url-pattern>*.action</url-pattern>
</servlet-mapping>
3、开发action

@Controller
public class HelloAction{
	@RequestMapping(value="/hello")
	public String helloMethod(Model model) throws Exception{
		System.out.println("HelloAction::helloMethod()");
		model.addAttribute("message","这是我的第二个springmvc应用程序");
		return "/success.jsp";
	}	
}
4、新建jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>这是我的第二个springmvc应用程序</title>
  </head>
  <body>
	success.jsp<br/>
	${message}
  </body>
</html>
5、配置springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
	  xmlns:mvc="http://www.springframework.org/schema/mvc"		
      xsi:schemaLocation="	
	  http://www.springframework.org/schema/beans 
	  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd	  
	  http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd   
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd       
      ">
	  <!-- Action控制器 -->
	  <context:component-scan base-package="cn.itcast.javaee.springmvc.helloannotation"/>  	
           
      <!-- 基于注解的映射器(可选) -->
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
      
      <!-- 基于注解的适配器(可选) -->
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
      
      <!-- 视图解析器(可选) -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
      	
</beans>

二、一个Action中,写多个类似的业务控制方法

package cn.itcast.action;

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

@Controller
@RequestMapping(value="/day02")
public class Day02Action {
	@RequestMapping(value="/add")
	public String add(Model model){
		model.addAttribute("message","添加成功");
		return "/day02.jsp";
	}
	@RequestMapping(value="/update")
	public String update(Model model){
		model.addAttribute("message","更新成功");
		return "/day02.jsp";
	}
	@RequestMapping(value="/delete")
	public String delete(Model model){
		model.addAttribute("message","删除成功");
		return "/day02.jsp";
	}

}
三、 限定某个业务控制方法,只允许GET或POST请求方式访问

可以在业务控制方法前,指明该业务控制方法只能接收GET或POST的请求(不指定则代表get/post方法均可)

@Controller
@RequestMapping(value="/user")
public class UserAction{
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Model model,int id,String name,double sal) throws Exception{
		System.out.println("HelloAction::add()::POST");
		System.out.println(id + ":" + name + ":" + sal);
		model.addAttribute("message","增加用户");
		return "/success.jsp";
	}	
}
四、在业务控制方法中写入Request,Response等传统web参数(不提倡,耦合)

@Controller
@RequestMapping(value="/user")
public class UserAction{
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(HttpServletRequest request,HttpServletResponse response) throws Exception{
		System.out.println("HelloAction::add()::POST");
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		double sal = Double.parseDouble(request.getParameter("sal"));
		System.out.println(id + ":" + name + ":" + sal);
		request.getSession().setAttribute("id",id);
		request.getSession().setAttribute("name",name);
		request.getSession().setAttribute("sal",sal);
		response.sendRedirect(request.getContextPath()+"/register.jsp");
	}	
}
五、使用@InitBind来解决字符串转日期类型

@InitBinder

   protected void initBinder(HttpServletRequestrequest,ServletRequestDataBinder binder) throws Exception {

      binder.registerCustomEditor(Date.class, newCustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));

   }

六、以实体的形式收集客户端参数

1、创建实体类

2、添加 controller和requestmapping注解

3、在方法中传入实体参数

4、需要注意的是jsp页面的name和实体属性要相同

@Controller
@RequestMapping(value="/day02")
public class Day02Action {
	
	@RequestMapping(value="/add")
	public String add(Model model,Student student){
		System.out.println(student.toString());
		model.addAttribute("message","添加成功");
		return "/day02.jsp";
	}
}
七、需要收集多个模型参数

例如:需要在一个表单中提交两个实体的相同属性(学生name和教师name),则需要区分两个name,即student.name和teacher.name。需要用一个新的实体(模型)将student和teacher再封装一次。
1、实体类

package cn.itcast.entity;

import java.util.Date;

public class Student {
	int id;
	String name;
	String pass;
	Date date;
	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 String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", pass=" + pass
				+ ", date=" + date + "]";
	}
	
	
}
package cn.itcast.entity;

import java.util.Date;

public class Teacher {
	int id;
	String name;
	String pass;
	Date date;
	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 String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + ", pass=" + pass
				+ ", date=" + date + "]";
	}
	
	
}
package cn.itcast.entity;

public class Bean {
	private Student student;
	private Teacher teacher;
	
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	@Override
	public String toString() {
		return "Bean [student=" + student + ", teacher=" + teacher + "]";
	}
	
}
2、jsp页面
<body>
	<form action="day02/add.do" method="post">
		id:<input id="id" name="student.id" type="text"/><br/>
		姓名:<input id="name" name="student.name" type="text"/><br/>
		密码:<input id="pass" name="student.pass" type="text"/><br/>
		日期:<input id="date" name="student.date" type="text"/><br/>
		id:<input id="id" name="teacher.id" type="text"/><br/>
		姓名:<input id="name" name="teacher.name" type="text"/><br/>
		密码:<input id="pass" name="teacher.pass" type="text"/><br/>
		日期:<input id="date" name="teacher.date" type="text"/><br/>
		
 		<input type="submit"/>
	</form>
</body>

3、action处理页面

@RequestMapping(value="/add")
	public String add(Model model,Bean bean){
		System.out.println("学生:"+bean.getStudent().toString());
		System.out.println("教师:"+bean.getTeacher().toString());
		model.addAttribute("message","添加成功");
		return "/day02.jsp";
	}

4、jsp展示页面

<body>
		学生id:<input id="id" value="${bean.student.id}" type="text"/><br/>
		学生姓名:<input id="name" value="${bean.student.name}" type="text"/><br/>
		学生密码:<input id="pass" value="${bean.student.pass}" type="text"/><br/>
		学生日期:<input id="date" value="${bean.student.date}" type="text"/><br/>
		教师id:<input id="id" value="${bean.teacher.id}" type="text"/><br/>
		教师姓名:<input id="name" value="${bean.teacher.name}" type="text"/><br/>
		教师密码:<input id="pass" value="${bean.teacher.pass}" type="text"/><br/>
		教师日期:<input id="date" value="${bean.teacher.date}" type="text"/><br/>		
</body>

八、在业务控制方法中收集数组参数

1、jsp页面,name需要相同,springmvc自动获取选中的value

<form action="day02/update.do" method="post">
	<p><input type="checkbox" name="category" value="今日话题" />今日话题 </p>   
    	<p><input type="checkbox" name="category" value="视觉焦点" />视觉焦点</p>
    	<p><input type="checkbox" name="category" value="财经" />财经</p>    
    	<p><input type="checkbox" name="category" value="汽车" />汽车</p>    
   	<p><input type="checkbox" name="category" value="科技" />科技</p>    
    	<p><input type="checkbox" name="category" value="房产" />房产</p>    
    	<p><input type="checkbox" name="category" value="旅游" />旅游</p>    
    	<p><input type="submit">
</form>
2、action方法

@RequestMapping(value="/get")
	public voidupdate(Model model,String[] category){
		for(String cat : category){
			System.out.print(cat+" ");
		}
	}
九、在业务控制方法中收集List<JavaBean>参数

@Controller
@RequestMapping(value="/user")
public class UserAction {
	@RequestMapping(value="/addAll")
	public String addAll(Bean bean,Model model) throws Exception{
		for(User user : bean.getUserList()){
			System.out.println(user.getName()+":"+user.getGender());
		}
		model.addAttribute("message","批量增加用户成功");
		return "/success.jsp";
	}
}
public class Bean {
	private List<User> userList = new ArrayList<User>();
	public Bean(){}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
}

<form action="${pageContext.request.contextPath}/user/addAll.action" method="POST"> 
		 
		姓名:<input type="text" name="userList[0].name" value="哈哈"/>
		性别:<input type="text" name="userList[0].gender" value="男"/>
		<hr/>
		
		姓名:<input type="text" name="userList[1].name" value="呵呵"/>
		性别:<input type="text" name="userList[1].gender" value="男"/>
		<hr/>

		姓名:<input type="text" name="userList[2].name" value="嘻嘻"/>
		性别:<input type="text" name="userList[2].gender" value="女"/>
		<hr/>
		
		<input type="submit" value="批量注册"/>
		
	</form>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值