码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC--提交表单

时间:2019-08-23 23:57:46      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:映射   返回   hello   todo   path   over   extend   ida   this   

今天使用AbstractCommandController做一个提交表单的样例

(1)首先,建立一个User.java

package com.zk.domain;

import java.util.Date;

public class User {
	private Integer id;
	private String name;
	private String age;
	private String birthday;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
}

(2)第二步,实现ToAddController.java,跳转至add.jsp,

package com.zk.UserController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class ToAddController implements Controller{

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView mv=new ModelAndView();
		mv.setViewName("add");
		return mv;
	}
} 

(3)第三步,实现CommandController.java

package com.zk.UserController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.zk.domain.User;

//从页面接受参数,封装javaBean的User对象
public class CommandController extends AbstractCommandController{
	//指定把参数封装到那个对象。
	public CommandController(){
		this.setCommandClass(User.class);
	}
	@Override
	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		// TODO Auto-generated method stub
		//值被封装命令对象
		User user=(User) command;
		
		ModelAndView mv=new ModelAndView();
		//设置models
		mv.addObject("user",user);
		//指定返回视图界面
		mv.setViewName("index");
		return mv;
	}
}

 (4)第四步,配置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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
         <!-- 配置处理器映射器 -->
         <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
     	 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
     	 <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
         <!-- 命令对象 -->
         <bean id="/command.do" class="com.zk.UserController.CommandController"></bean>
         <!-- 跳转到Add页面 -->
         <bean id="/toAdd.do" class="com.zk.UserController.ToAddController"></bean>  
         <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/jsp/"></property>
         <property name="suffix" value=".jsp"></property>
         </bean>
         
</beans>

(5)两个jsp界面

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
  <form action="${pageContext.request.contextPath }/command.do">
   Id:<input type="text" name="id"  id="id"/><br>
   name:<input type="text" name="name" id="name"/><br>
   age:<input type="text" name="age" id="age"/><br>
   birthday:<input type="text" name="birthday" id="birthday"/><br>
  <input type="submit" value="提交" />
  </form>
  </body>
</html>

  index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 ‘index.jsp‘ starting page</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<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>
  <!-- ${hello } --> 
  <h1>${user.name}||${user.id}||${user.age}||${user.birthday}</h1> 
  </body>
</html>

  运行结果如下:

技术图片

http://ms-20170731tkkc:8080/SpringMVC_004/command.do?id=1&name=zk&age=24&birthday=1995-03-04

带过来的参数

技术图片

 

SpringMVC--提交表单

标签:映射   返回   hello   todo   path   over   extend   ida   this   

原文地址:https://www.cnblogs.com/longlyseul/p/11403186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!