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

Spring MVC - 表单处理示例

时间:2017-12-02 00:44:40      阅读:447      评论:0      收藏:0      [点我收藏+]

标签:项目   mha   年龄   user   项目文件   one   label   oca   package   

环境搭建

环境:

  • 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 - 表单处理示例

标签:项目   mha   年龄   user   项目文件   one   label   oca   package   

原文地址:http://www.cnblogs.com/ktao/p/7944781.html

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