以下示例显示如何在使用spring web mvc框架的表单中使用列表框(listbox)。首先使用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;
private string favoritenumber;
private string country;
private string [] skills;
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;
}
public string getgender() {
return gender;
}
public void setgender(string gender) {
this.gender = gender;
}
public string getfavoritenumber() {
return favoritenumber;
}
public void setfavoritenumber(string favoritenumber) {
this.favoritenumber = favoritenumber;
}
public string getcountry() {
return country;
}
public void setcountry(string country) {
this.country = country;
}
public string[] getskills() {
return skills;
}
public void setskills(string[] skills) {
this.skills = skills;
}
}
usercontroller.java 的代码如下所示 -
package com.h3.springmvc;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
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());
model.addattribute("favoritenumber", user.getfavoritenumber());
model.addattribute("country", user.getcountry());
model.addattribute("skills", user.getskills());
return "userlist";
}
@modelattribute("webframeworklist")
public list<string> getwebframeworklist()
{
list<string> webframeworklist = new arraylist<string>();
webframeworklist.add("spring mvc");
webframeworklist.add("struts 1");
webframeworklist.add("struts 2");
webframeworklist.add("apache hadoop");
return webframeworklist;
}
@modelattribute("numberslist")
public list<string> getnumberslist()
{
list<string> numberslist = new arraylist<string>();
numberslist.add("1");
numberslist.add("2");
numberslist.add("3");
numberslist.add("4");
return numberslist;
}
@modelattribute("countrylist")
public map<string, string> getcountrylist()
{
map<string, string> countrylist = new hashmap<string, string>();
countrylist.put("us", "united states");
countrylist.put("ch", "china");
countrylist.put("sg", "singapore");
countrylist.put("my", "malaysia");
return countrylist;
}
@modelattribute("skillslist")
public map<string, string> getskillslist()
{
map<string, string> skilllist = new hashmap<string, string>();
skilllist.put("hibernate", "hibernate");
skilllist.put("spring", "spring");
skilllist.put("apache hadoop", "apache hadoop");
skilllist.put("struts", "struts");
return skilllist;
}
}
这里的第一个服务方法user()
,我们已经在modelandview
对象中传递了一个名称为“command
”的空user
对象,因为如果在jsp文件中使用<form:form>
标签,spring框架需要一个名称为“command
”的对象。 所以当调用user()
方法时,它返回user.jsp
视图。
第二个服务方法adduser()
将根据url => listbox/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="/listbox/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><form:label path="favoriteframeworks">喜欢的框架/技术:</form:label></td>
<td><form:checkboxes items="${webframeworklist}"
path="favoriteframeworks" /></td>
</tr>
<tr>
<td><form:label path="gender">性别:</form:label></td>
<td><form:radiobutton path="gender" value="m" label="男" /> <form:radiobutton
path="gender" value="f" label="女" /></td>
</tr>
<tr>
<td><form:label path="favoritenumber">喜欢的数字:</form:label></td>
<td><form:radiobuttons path="favoritenumber"
items="${numberslist}" /></td>
</tr>
<tr>
<td><form:label path="country">所在国家:</form:label></td>
<td><form:select path="country">
<form:option value="none" label="请选择..." />
<form:options items="${countrylist}" />
</form:select></td>
</tr>
<tr>
<td><form:label path="skills">技术:</form:label></td>
<td><form:select path="skills" items="${skillslist}"
multiple="true" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" /></td>
</tr>
</table>
</form:form>
</body>
</html>
这里使用<form:select />
及其属性 multiple=true
标签来呈现html列表多选框。 例如 -
<form:select path="skills" items="${skillslist}" multiple="true" />
它将呈现以下html内容。
<select id="skills" name="skills" multiple="multiple">
<option value="struts">struts</option>
<option value="hibernate">hibernate</option>
<option value="apache wicket">apache hadoop</option>
<option value="spring">spring</option>
</select>
<input type="hidden" name="_skills" value="1"/>
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>
<tr>
<td>喜欢的数字:</td>
<td>${favoritenumber}</td>
</tr>
<tr>
<td>国家:</td>
<td>${country}</td>
</tr>
<tr>
<td>技术:</td>
<td>
<%
string[] skills = (string[]) request.getattribute("skills");
for (string skill : skills) {
out.println(skill);
}
%>
</td>
</tr>
</table>
</body>
</html>
完成创建源和配置文件后,发布应用程序到tomcat服务器。
现在启动tomcat服务器,现在尝试访问url => http://localhost:8080/listbox/user ,如果spring web应用程序没有问题,应该看到以下结果:
提交所需信息后,点击提交按钮提交表单。 如果spring web应用程序没有问题,应该看到以下结果: