以下示例显示如何在使用spring web mvc框架的表单中使用单选按钮(radiobutton)。首先使用eclipse ide来创建一个web工程,并按照以下步骤使用spring web framework开发基于动态表单的web应用程序:
com.h3.springmvc
包下创建两个java类user
,usercontroller
。jsp
子文件夹下创建两个视图文件:user.jsp
,userlist.jsp
。完整的项目文件目录结构如下所示 -
user.java 的代码如下所示 -
package com.h3.springmvc;
public class user {
private string username;
private string password;
private string address;
private boolean receivepaper;
private string [] favoriteframeworks;
private string gender;
public string getgender() {
return gender;
}
public void setgender(string gender) {
this.gender = gender;
}
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this.password = password;
}
public string getaddress() {
return address;
}
public void setaddress(string address) {
this.address = address;
}
public boolean isreceivepaper() {
return receivepaper;
}
public void setreceivepaper(boolean receivepaper) {
this.receivepaper = receivepaper;
}
public string[] getfavoriteframeworks() {
return favoriteframeworks;
}
public void setfavoriteframeworks(string[] favoriteframeworks) {
this.favoriteframeworks = favoriteframeworks;
}
}
usercontroller.java 的代码如下所示 -
package com.h3.springmvc;
import java.util.arraylist;
import java.util.list;
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 usercontroller {
@requestmapping(value = "/user", method = requestmethod.get)
public modelandview user() {
user user = new user();
user.setfavoriteframeworks((new string []{"spring mvc","struts 2"}));
user.setgender("m");
modelandview modelandview = new modelandview("user", "command", user);
return modelandview;
}
@requestmapping(value = "/adduser", method = requestmethod.post)
public string adduser(@modelattribute("springweb")user user,
modelmap model) {
model.addattribute("username", user.getusername());
model.addattribute("password", user.getpassword());
model.addattribute("address", user.getaddress());
model.addattribute("receivepaper", user.isreceivepaper());
model.addattribute("favoriteframeworks", user.getfavoriteframeworks());
model.addattribute("gender", user.getgender());
return "userlist";
}
@modelattribute("webframeworklist")
public list<string> getwebframeworklist()
{
list<string> webframeworklist = new arraylist<string>();
webframeworklist.add("spring mvc");
webframeworklist.add("spring boot");
webframeworklist.add("struts 2");
webframeworklist.add("apache hadoop");
return webframeworklist;
}
}
这里的第一个服务方法user()
,我们已经在modelandview
对象中传递了一个名称为“command
”的空user
对象,因为如果在jsp文件中使用<form:form>
标签,spring框架需要一个名称为“command
”的对象。 所以当调用user()
方法时,它返回user.jsp
视图。
第二个服务方法adduser()
将根据url => radiobutton/adduser
上的post方法请求时调用。根据提交的信息准备模型对象。 最后从服务方法返回“userlist
”视图,这将呈现userlist.jsp
视图。
user.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>
<form:form method="post" action="/radiobutton/adduser">
<table>
<tr>
<td><form:label path="username">用户名:</form:label></td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td><form:label path="password">密码:</form:label></td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><form:label path="address">地址:</form:label></td>
<td><form:textarea path="address" rows="5" cols="30" /></td>
</tr>
<tr>
<td><form:label path="receivepaper">订阅新闻?</form:label></td>
<td><form:checkbox path="receivepaper" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
这里使用<form:radiobutton />
标签来呈现html密码框。 例如 -
<form:radiobutton path="gender" value="m" label="男" />
<form:radiobutton path="gender" value="f" label="女" />
它将呈现以下html内容。
<input id="gender1" name="gender" type="radio" value="m" checked="checked"/><label for="gender1">男</label>
<input id="gender2" name="gender" type="radio" value="f"/><label for="gender2">女</label>
userlist.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>${username}</td>
</tr>
<tr>
<td>密码:</td>
<td>${password}</td>
</tr>
<tr>
<td>地址:</td>
<td>${address}</td>
</tr>
<tr>
<td>是否订阅:</td>
<td>${receivepaper}</td>
</tr>
<tr>
<td>喜欢的技术/框架:</td>
<td>
<%
string[] favoriteframeworks = (string[]) request.getattribute("favoriteframeworks");
for (string framework : favoriteframeworks) {
out.println(framework);
}
%>
</td>
</tr>
<tr>
<td>性别:</td>
<td>${(gender=="m"? "男" : "女")}</td>
</tr>
</table>
</body>
</html>
完成创建源和配置文件后,发布应用程序到tomcat服务器。
现在启动tomcat服务器,现在尝试访问url => http://localhost:8080/radiobutton/user ,如果spring web应用程序没有问题,应该看到以下结果:
提交所需信息后,点击提交按钮提交表单。 如果spring web应用程序没有问题,应该看到以下结果: