以下示例演示如何编写一个简单的基于web的应用程序,它使用spring web mvc框架使用html表单。 首先使用eclipse ide,并按照以下步骤使用spring web framework开发基于动态表单的web应用程序:
formhandling
,并创建一个包名称为 com.h3.springmvc
。com.h3.springmvc
包下创建两个java类student
,studentcontroller
。jsp
子文件夹下创建两个视图文件student.jsp
,result.jsp
。完整的项目文件结构如下所示 -
student.java文件中的代码内容 -
package com.h3.springmvc;
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.h3.springmvc;
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("springweb")student student,
modelmap model) {
model.addattribute("name", student.getname());
model.addattribute("age", student.getage());
model.addattribute("id", student.getid());
return "result";
}
}
这里的第一个服务方法student()
,我们已经在modelandview
对象中传递了一个名为“command
”的空对象,因为如果在jsp中使用<form:form>
标签,spring框架需要一个名为“command
”的对象文件。 所以当调用student()
方法时,它返回student.jsp
视图。
第二个服务方法addstudent()
将在 urlhelloweb/addstudent
上的post方法提交时调用。将根据提交的信息准备模型对象。最后,将从服务方法返回“result
”视图,这将最终渲染result.jsp
视图。
student.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>student information</h2>
<form:form method="post" action="/formhandling/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>
完成创建源和配置文件后,导出应用程序。 右键单击应用程序,并使用导出> war文件选项,并将 formhandling.war
文件保存在tomcat的webapps
文件夹中。或者直接右键选择“run as -> run on server”。
启动tomcat服务器,并确保您能够使用标准浏览器从webapps文件夹访问其他网页。现在尝试url => http://localhost:8080/formhandling/student ,如果spring web应用程序没有问题,那么应该看到以下结果:
提交所需信息后,点击提交按钮提交表单。 如果spring web应用程序没有问题,应该看到以下结果:
控制器里面的代码更改如下: requestmappingvalue = "/student" method = requestmethod.get public modelandview studentmodelattribute"student"student student { return new modelandview"student" "student"new student }不然会报错如下:严重:bindingresult和bean名称'command'的普通目标对象都不可用作请求属性java.lang.illegalstateexception:在org.springframework.web.servlet.support中,bindingresult和bean名称'command'的普通目标对象都不可用作请求属性.bindstatus。<初始化>(bindstatus.java:142) 提交时间:2019-08-23