在这个例子里你将会看到如何使用动态值来填充表单。
注册表单registration.jsp页面.
- <span style="font-size:16px;"><%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Registration Page</title>
- </head>
- <body>
- <form:form method="post" commandName="user">
- User Name :<form:input path="name"/><br>
- Password :<form:password path="password"/><br>
- Gender :<form:radiobutton path="gender" value="Male" label="Male"/>
- <form:radiobutton path="gender" value="Female" label="Female"/><br>
- Country :<form:select path="country">
- <form:option value="0" label="Select" />
- <form:options items="${countryList}" itemValue="countryId" itemLabel="countryName"/>
- </form:select><br>
- About you :<form:textarea path="aboutYou"/><br>
- Community :<form:checkboxes path="communityList" items="${communityList}" itemValue="key" itemLabel="value" /> <br>
- <form:checkbox path="mailingList" label="Would you like to join our mailinglist?"/><br>
- <input type="submit" value="Register">
- </form:form>
- </body>
- </html></span>
在表单里我们从后台填充countryList和communityList。items属性持有他们的集合。itemValue 和itemValue 分别持用key和对应的value。
表单中我们有三个动态对象User,Country和Community,User对象是和表单相关联的》
User类:
- package com.zcl.spring.formtag;
- import java.util.List;
- @SuppressWarnings("unchecked")
- public class User {
- private String name ;
- private String password ;
- private String gender ;
- private String country ;
- private List countryList ;
- private String aboutYou ;
- private String[] community ;
- private List communityList ;
- private boolean mailingList ;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- public List getCountryList() {
- return countryList;
- }
- public void setCountryList(List countryList) {
- this.countryList = countryList;
- }
- public String getAboutYou() {
- return aboutYou;
- }
- public void setAboutYou(String aboutYou) {
- this.aboutYou = aboutYou;
- }
- public String[] getCommunity() {
- return community;
- }
- public void setCommunity(String[] community) {
- this.community = community;
- }
- public List getCommunityList() {
- return communityList;
- }
- public void setCommunityList(List communityList) {
- this.communityList = communityList;
- }
- public boolean isMailingList() {
- return mailingList;
- }
- public void setMailingList(boolean mailingList) {
- this.mailingList = mailingList;
- }
- }
User对象中countryList和communityList分别持有countries和communities的list集合。
CountryList包含了一系列Country对象
- package com.zcl.spring.formtag;
- public class Country {
- private int countryId ;
- private String countryName ;
- public Country(int countryId, String countryName) {
- this.countryId = countryId;
- this.countryName = countryName;
- }
- public int getCountryId() {
- return countryId;
- }
- public void setCountryId(int countryId) {
- this.countryId = countryId;
- }
- public String getCountryName() {
- return countryName;
- }
- public void setCountryName(String countryName) {
- this.countryName = countryName;
- }
- }
相似的communityList包含了一系列community对象
- package com.zcl.spring.formtag;
- public class Community {
- private String key ;
- private String value ;
- public Community(String key, String value) {
- this.key = key;
- this.value = value;
- }
- public String getKey() {
- return key;
- }
- public void setKey(String key) {
- this.key = key;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- }
同样的这儿value用于在前台显示而key用于后台。
在控制器里需要覆写referenceData()方法,在这个方法里你能指定默认值当表单加载给用户时,这个方法被自动的调用。
- package com.zcl.spring.formtag;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.SimpleFormController;
- import com.sun.xml.internal.ws.developer.UsesJAXBContext;
- @SuppressWarnings("deprecation")
- public class UserController extends SimpleFormController {
- private UserService userService ;
- public UserController(){
- setCommandClass(User.class) ;
- setCommandName("user") ;
- }
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
- @SuppressWarnings("unchecked")
- @Override
- protected Map referenceData(HttpServletRequest request)throws Exception{
- Map referenceDate = new HashMap() ;
- referenceDate.put("countryList",userService.getAllCountries()) ;
- referenceDate.put("communityList", userService.getAllCommunities()) ;
- return referenceDate ;
- }
- @Override
- protected ModelAndView onSubmit(Object command) throws Exception{
- User user = (User)command ;
- userService.add(user) ;
- return new ModelAndView("userSuccess","user",user) ;
- }
- }
这儿我们需要在UserService和UserServiceImpl里增加相应的方法,设置相应的值。
- package com.zcl.spring.formtag;
- import java.util.List;
- public interface UserService {
- public void add(User user) ;
- public List<Country> getAllCountries();
- public List<Community> getAllCommunities();
- }
- package com.zcl.spring.formtag;
- import java.util.ArrayList;
- import java.util.List;
- public class UserServiceImpl implements UserService {
- @Override
- public void add(User user) {
- System.out.println("User added successfully") ;
- }
- @SuppressWarnings("unchecked")
- @Override
- public List getAllCountries() {
- List countryList = new ArrayList();
- countryList.add(new Country(1,"India"));
- countryList.add(new Country(2,"USA"));
- countryList.add(new Country(3,"UK"));
- return countryList;
- }
- @SuppressWarnings("unchecked")
- @Override
- public List getAllCommunities() {
- List communityList = new ArrayList();
- communityList.add(new Community("Spring","Spring"));
- communityList.add(new Community("Hibernate","Hibernate"));
- communityList.add(new Community("Struts","Struts"));
- return communityList;
- }
- }
配置dispatcher-servlet.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans">
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix">
- <value>/WEB-INF/jsp/</value>
- </property>
- <property name="suffix">
- <value>.jsp</value>
- </property>
- </bean>
- <bean id="userService" class="com.zcl.spring.formtag.UserServiceImpl" />
- <!-- <context:component-scan base-package="com.zcl.spring.simpleform" />
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />-->
- <bean class="com.zcl.spring.formtag.UserController" p:successView="userSuccess" p:formView="userForm" p:userService-ref="userService" name="/userRegistration.html"/>
- </beans>
修改显示页面,使用jstl标签。下载jstl包将c.tld拷贝到WEB-INF下,然后就可以直接使用了,在jsp文件里加上<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>表示支持他的核心标签库。
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Success page</title>
- </head>
- <body>
- User Details
- <hr>
- User Name : <c:out value="${user.name}"></c:out> <br />
- Gender : <c:out value="${user.gender}"></c:out> <br />
- Country : <c:out value="${user.country}"></c:out> <br />
- About You : <c:out value="${user.aboutYou}"></c:out> <br />
- Community : <c:forEach var="community" items="${user.communityList}">
- <c:out value="${community}"></c:out>
- </c:forEach> <br />
- Mailing List: <c:out value="${user.mailingList} "></c:out>
- </body>
- </html>
运行结果和前面一样,注意要通过首页“response.sendRedirect("userRegistration.html"); ”跳转过去。