环境搭建

环境:

  • Intellij IDEA
  • Spring MVC

完整的项目文件结构如下所示:

Student.java

package com.ktao.controller;

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

StudentController.java

package com.ktao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView; @Controller
public class StudentController { @RequestMapping(value = "/student",method = RequestMethod.GET)
public ModelAndView student(){
return new ModelAndView("student","command",new Student());
} @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(Student student,ModelMap model) {
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId()); return "result";
}
}

配置文件

web.xml

FormHanding-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.ktao.controller"/> <!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

视图文件

student.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<jsp:useBean id="command" class="com.ktao.controller.Student" scope="request" ></jsp:useBean> <html>
<head>
<title>Spring MVC表单处理</title>
</head>
<body> <h2>Student Information</h2>
<form:form method="POST" action="addStudent">
<table>
<tr>
<td><form:label path="name">名字:</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="age">年龄:</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
单<td><form:label path="id">编号:</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="提交表单"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>

result.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring MVC表单处理</title>
</head>
<body> <h2>提交的学生信息如下 - </h2>
<table>
<tr>
<td>名称:</td>
<td>${name}</td>
</tr>
<tr>
<td>年龄:</td>
<td>${age}</td>
</tr>
<tr>
<td>编号:</td>
<td>${id}</td>
</tr>
</table>
</body>
</html>

运行结果

报错分析

错误1

错因:lib文件包不完整

解决方法:

错误2

javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

方法:

1为抛出异常原因,2为异常解决方法。

1. 原因: 进入spring:bind标签源码你可以看到

Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException("Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}

beanName= <spring:bind path="command.spjg">的绿色部分

如果你是直接对某个页面进行请求,那么request中还没command这个对象

2.

在页面上加上

<jsp:useBean id="command" class="com.mvc.domain.BlogForm" scope="request" ></jsp:useBean>

红色部分填上你的绑定类

解释一下,这个command类似于struts的引用的form对象,使用jsp:userBean是引用在spring-mvc.xml配置文件中的command对象。

Spring MVC - 表单处理示例的更多相关文章

  1. Spring MVC表单处理

    以下示例演示如何编写一个简单的基于Web的应用程序,它使用Spring Web MVC框架使用HTML表单. 首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework ...

  2. 使用Spring MVC表单标(转)

    概述     在低版本的Spring中,你必须通过JSTL或<spring:bind>将表单对象绑定到HTML表单页面中,对于习惯了Struts表单标签的开发者来说,Spring MVC的 ...

  3. Spring MVC表单标签

    从Spring 2.0开始,Spring MVC开始全面支持表单标签,通过Spring MVC表单标签,我们可以很容易地将控制器相关的表单对象绑定到HTML表单元素中. form标签     和使用任 ...

  4. Spring MVC表单提交

    实际应用中,列表中的单条记录的修改,可能需要传很多对象参数到后台服务器,Spring MVC表单标签<form:> 提供了一种简洁的提交方式. <form id="form ...

  5. [Spring MVC] - 表单提交

    Spring MVC自带的表单标签比较简单,很多时候需要借助EL和JSTL来完成. 下面是一个比较简单的表单提交页面功能: 1.User model package com.my.controller ...

  6. Spring MVC表单防重复提交

    利用Spring MVC的过滤器及token传递验证来实现表单防重复提交. 创建注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RU ...

  7. spring mvc表单的展现、输入处理、校验的实现

    之前已经实现了spring mvc的入门例子及如何处理带参数的请求Controller编写.本文主要记录: 1)重定向请求 2)处理路径中含有变量的请求 3)使用JSR-303进行校验 ① 首先,编写 ...

  8. spring mvc表单form值自动传到javabean-注解@ModelAttribute

    直接通过Form Bean进行表单可以简化表单提交的处理,特别是对于复杂表单,过于简单的表单就不建议了,因为毕竟需要额外创建一个Form Bean.前段时间项目中有一个比较复杂的表单,有多层次而且涉及 ...

  9. Spring MVC 表单校验 (七)

    完整的项目案例: springmvc.zip 目录 实例 除了依赖spring-webmvc还需要依赖jackson-databind(用于转换json数据格式)和hibernate-validato ...

随机推荐

  1. sql 多条记录插入

    --多条记录插入,用逗号分开值. INSERT dbo.studentinfor ( id, name, class, age, hpsw ) ', -- id - nvarchar(50) N'te ...

  2. thinkphp后台向前台传值没有传过去的小问题

    if($listyyarr){ $this->assign('listyyarr',$listyyarr); //$this->assign('nowDated',$endDated); ...

  3. 分布式系统之消息中间件rabbitmq

    分布式系统之消息中间件rabbitmq 博客分类: 感谢:  一般php 用rabbitmq  java 用activemq  http://spartan1.iteye.com/blog/11802 ...

  4. NATAPP 内网映射,Visual Studio ,C# 实现本地开发微信公众号,本地调试无需服务器

    点击软件安装教程,根据安装教程,注册帐号,下载软件,配置软件.配置完后如下图,途中红色位置免费版本是随机的. 红色位置是自己的映射域名. 打开VS,并且打开项目,右键项目,在web 选项中修改项目UR ...

  5. GIT入门笔记(20)- 使用eclipse 基于 git 开发过程梳理

    一.创建本地分支 1.下载/更新 本地 主干 如果本地还没有 本地主干,下载:git clone 如果本地已有了 本地主干,更新:git pull 工程右键菜单:team -> pull 2.基 ...

  6. GIT入门笔记(6)- 向版本库添加文本文件

    1.把文本文件添加到版本库? 所有的版本控制系统,其实只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等等,Git也不例外. 版本控制系统可以告诉你每次的改动,比如在第5行加了一个单词&q ...

  7. C# Bootstrap table之 分页

    效果如图: 一.声明talbe <div class="container"> <table id="table" class="t ...

  8. 前端开发必备之Chrome开发者工具(一)

    本文介绍的 Chrome 开发者工具基于 Chrome 65版本,如果你的 Chrome 开发者工具没有下文提到的那些内容,请检查下 Chrome 的版本 简介 Chrome 开发者工具是一套内置于 ...

  9. Python之Scrapy爬虫框架 入门实例(一)

    一.开发环境 1.安装 scrapy 2.安装 python2.7 3.安装编辑器 PyCharm 二.创建scrapy项目pachong 1.在命令行输入命令:scrapy startproject ...

  10. vscode设置出错, 无法自动补全

    问题: 之前设置的没问题, vscode重装后, 发现vscode里面的设置还在, 但敲代码却无法识别虚拟环境中的包了, 因此相关的内容也无法自动补全. 解决: 后来发现, 实际上设置没有出错, 但重 ...