以下是一个简单的Spring MVC表单处理的例子,包括创建表单、提交表单数据,以及显示提交的数据。

1. 创建表单:

在src/main/webapp/WEB-INF/views目录下创建一个studentForm.jsp文件,用于显示表单:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Student Form</title>
</head>
<body>
    <h2>Student Information Form</h2>
    <form action="submitForm" method="post">
        <label for="firstName">First Name:</label>
        <input type="text" id="firstName" name="firstName" required/><br/>

        <label for="lastName">Last Name:</label>
        <input type="text" id="lastName" name="lastName" required/><br/>

        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required/><br/>

        <input type="submit" value="Submit"/>
    </form>
</body>
</html>

2. 创建实体类:

创建一个Student实体类,用于存储学生信息:
package com.example.model;

public class Student {

    private String firstName;
    private String lastName;
    private String email;

    // getters and setters
}

3. 创建Controller:

创建一个StudentController类,处理表单的显示和提交:
package com.example.controller;

import com.example.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class StudentController {

    @RequestMapping(value = "/showForm", method = RequestMethod.GET)
    public String showForm(Model model) {
        model.addAttribute("student", new Student());
        return "studentForm";
    }

    @RequestMapping(value = "/submitForm", method = RequestMethod.POST)
    public String submitForm(Student student, Model model) {
        // 处理提交的表单数据
        model.addAttribute("message", "Student Information Submitted Successfully!");
        return "studentResult";
    }
}

4. 创建结果页面:

在src/main/webapp/WEB-INF/views目录下创建一个studentResult.jsp文件,用于显示提交结果:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Student Result</title>
</head>
<body>
    <h2>${message}</h2>
    <a href="showForm">Go Back to Form</a>
</body>
</html>

5. 配置web.xml:

如果你的项目还没有web.xml文件,创建一个并添加如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <display-name>SpringMVCFormExample</display-name>

    <!-- 配置DispatcherServlet -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

6. 配置spring-mvc-servlet.xml:

在src/main/webapp/WEB-INF目录下创建一个spring-mvc-servlet.xml文件,配置Spring MVC:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.example.controller" />

    <!-- 开启注解驱动 -->
    <mvc:annotation-driven />

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

7. 运行:

在你的IDE中运行或将项目打包并部署到支持Servlet容器的环境中。访问http://localhost:8080/your-project-name/showForm,填写表单并提交后应该能看到提交结果。

这是一个简单的Spring MVC表单处理的例子,演示了创建表单、提交表单数据,以及显示提交的数据的基本流程。根据实际需求,你可以进一步扩展和定制表单处理。


转载请注明出处:http://www.zyzy.cn/article/detail/6969/Spring