zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java web开发(编写spring mvc下的表单处理程序)

JAVASpringWebMVC开发 编写 表单 处理程序
2023-09-27 14:27:10 时间

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

        之前讨论过jsp是怎么把数据传递给servlet的,那么在spring mvc框架下面表单数据一般怎么处理呢?本文参考了这个范例,https://www.w3cschool.cn/wkspring/v41f1mmk.html。今天正好借着IDEA工具,实现一下这个功能。

1、利用IDEA创建一个spring mvc工程

2、工程本身命名为Hello,确定完成

3、配置好Tomcat

        有两个部分需要配置下,一个是端口,不妨配置为8092。另外一个在deployment里面,也需要配置下。

4、打开File->Project structure,添加lib文件,单击右下角的fix按钮即可,

5、启动tomcat服务器,如果可以看到index.jsp网页的内容,那说明一切正常

6、将web.xml中的*.form修改为/

7、修改dispatch-servlet.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <context:component-scan base-package="com.tutorialspoint" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

8、在src目录下面创建com.tutorialspoint package

8.1 创建Student.java

package com.tutorialspoint;

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;
    }
}

8.2 编写StudentController.java文件

package com.tutorialspoint;

import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
@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(@ModelAttribute("Hello_war_exploded")Student student,
                             ModelMap model) {
        model.addAttribute("name", student.getName());
        model.addAttribute("age", student.getAge());
        model.addAttribute("id", student.getId());
        return "result";
    }
}

        这里的Hello_war_exploded是最终要生成war文件时的名字,这个是需要注意一下的。

9、在WEB/WEB-INF创建一个jsp目录

9.1 创建student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="addStudent">
   <table>
    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age" /></td>
    </tr>
    <tr>
        <td><form:label path="id">id</form:label></td>
        <td><form:input path="id" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>
</body>
</html>

9.2 创建result.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Submitted Student Information</h2>
   <table>
    <tr>
        <td>Name</td>
        <td>${name}</td>
    </tr>
    <tr>
        <td>Age</td>
        <td>${age}</td>
    </tr>
    <tr>
        <td>ID</td>
        <td>${id}</td>
    </tr>
</table>  
</body>
</html>

10、所有条件都准备好之后,就可以开始调试了

        输入localhost:8092/Hello_war_exploded/student之后,就可以看到这样的网页,

         不妨输入tom、20、1,单击submit提交之后,就可以看到这样的网页,

        如果我们可以正确地看到上面这两项内容,基本说明网页部分是没有问题的。如果出现的结果有错误,那么就要回头查一下,是哪个地方出了问题。