Spring Form Tags

在这个例子里你将会看到如何使用动态值来填充表单。

注册表单registration.jsp页面.

[html]  view plain copy
  1. <span style="font-size:16px;"><%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3.  <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  8. <title>Registration Page</title>  
  9. </head>  
  10. <body>  
  11.     <form:form method="post" commandName="user">  
  12.         User Name :<form:input path="name"/><br>  
  13.         Password :<form:password path="password"/><br>  
  14.         Gender :<form:radiobutton path="gender" value="Male" label="Male"/>  
  15.                 <form:radiobutton path="gender" value="Female" label="Female"/><br>  
  16.         Country :<form:select path="country">  
  17.             <form:option value="0" label="Select" />  
  18.             <form:options items="${countryList}" itemValue="countryId" itemLabel="countryName"/>  
  19.         </form:select><br>  
  20.         About you :<form:textarea path="aboutYou"/><br>  
  21.         Community :<form:checkboxes path="communityList" items="${communityList}" itemValue="key" itemLabel="value" /> <br>  
  22.         <form:checkbox path="mailingList" label="Would you like to join our mailinglist?"/><br>  
  23.         <input type="submit" value="Register">  
  24.     </form:form>  
  25. </body>  
  26. </html></span>  

在表单里我们从后台填充countryList和communityList。items属性持有他们的集合。itemValue 和itemValue 分别持用key和对应的value

表单中我们有三个动态对象User,Country和Community,User对象是和表单相关联的》

User类:

[java]  view plain copy
  1. package com.zcl.spring.formtag;  
  2.   
  3. import java.util.List;  
  4.   
  5. @SuppressWarnings("unchecked")  
  6. public class User {  
  7.     private String name ;  
  8.     private String password ;  
  9.     private String gender ;  
  10.     private String country ;  
  11.     private List countryList ;  
  12.     private String aboutYou ;  
  13.     private String[] community ;  
  14.     private List communityList ;  
  15.     private boolean mailingList ;  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.     public String getPassword() {  
  23.         return password;  
  24.     }  
  25.     public void setPassword(String password) {  
  26.         this.password = password;  
  27.     }  
  28.     public String getGender() {  
  29.         return gender;  
  30.     }  
  31.     public void setGender(String gender) {  
  32.         this.gender = gender;  
  33.     }  
  34.     public String getCountry() {  
  35.         return country;  
  36.     }  
  37.     public void setCountry(String country) {  
  38.         this.country = country;  
  39.     }  
  40.     public List getCountryList() {  
  41.         return countryList;  
  42.     }  
  43.     public void setCountryList(List countryList) {  
  44.         this.countryList = countryList;  
  45.     }  
  46.     public String getAboutYou() {  
  47.         return aboutYou;  
  48.     }  
  49.     public void setAboutYou(String aboutYou) {  
  50.         this.aboutYou = aboutYou;  
  51.     }  
  52.     public String[] getCommunity() {  
  53.         return community;  
  54.     }  
  55.     public void setCommunity(String[] community) {  
  56.         this.community = community;  
  57.     }  
  58.     public List getCommunityList() {  
  59.         return communityList;  
  60.     }  
  61.     public void setCommunityList(List communityList) {  
  62.         this.communityList = communityList;  
  63.     }  
  64.     public boolean isMailingList() {  
  65.         return mailingList;  
  66.     }  
  67.     public void setMailingList(boolean mailingList) {  
  68.         this.mailingList = mailingList;  
  69.     }  
  70. }  

User对象中countryList和communityList分别持有countries和communities的list集合。

CountryList包含了一系列Country对象

[java]  view plain copy
  1. package com.zcl.spring.formtag;  
  2.   
  3. public class Country {  
  4.     private int countryId ;  
  5.     private String countryName ;  
  6.       
  7.     public Country(int countryId, String countryName) {  
  8.         this.countryId = countryId;  
  9.         this.countryName = countryName;  
  10.     }  
  11.     public int getCountryId() {  
  12.         return countryId;  
  13.     }  
  14.     public void setCountryId(int countryId) {  
  15.         this.countryId = countryId;  
  16.     }  
  17.     public String getCountryName() {  
  18.         return countryName;  
  19.     }  
  20.     public void setCountryName(String countryName) {  
  21.         this.countryName = countryName;  
  22.     }  
  23. }  
contryId用于在后台找到country而 countryName 用于在前台显示country。

相似的communityList包含了一系列community对象

[java]  view plain copy
  1. package com.zcl.spring.formtag;  
  2.   
  3. public class Community {  
  4.     private String key ;  
  5.     private String value ;  
  6.     public Community(String key, String value) {  
  7.         this.key = key;  
  8.         this.value = value;  
  9.     }  
  10.     public String getKey() {  
  11.         return key;  
  12.     }  
  13.     public void setKey(String key) {  
  14.         this.key = key;  
  15.     }  
  16.     public String getValue() {  
  17.         return value;  
  18.     }  
  19.     public void setValue(String value) {  
  20.         this.value = value;  
  21.     }  
  22. }  

同样的这儿value用于在前台显示而key用于后台。

在控制器里需要覆写referenceData()方法,在这个方法里你能指定默认值当表单加载给用户时,这个方法被自动的调用。

[html]  view plain copy
  1. package com.zcl.spring.formtag;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import javax.servlet.http.HttpServletRequest;  
  6.   
  7. import org.springframework.web.servlet.ModelAndView;  
  8. import org.springframework.web.servlet.mvc.SimpleFormController;  
  9.   
  10. import com.sun.xml.internal.ws.developer.UsesJAXBContext;  
  11.   
  12. @SuppressWarnings("deprecation")  
  13. public class UserController extends SimpleFormController {  
  14.     private UserService userService ;  
  15.       
  16.     public UserController(){  
  17.         setCommandClass(User.class) ;  
  18.         setCommandName("user") ;  
  19.     }  
  20.   
  21.     public void setUserService(UserService userService) {  
  22.         this.userService = userService;  
  23.     }   
  24.       
  25.     @SuppressWarnings("unchecked")  
  26.     @Override  
  27.     protected Map referenceData(HttpServletRequest request)throws Exception{  
  28.         Map referenceDate = new HashMap() ;  
  29.         referenceDate.put("countryList",userService.getAllCountries()) ;  
  30.         referenceDate.put("communityList", userService.getAllCommunities()) ;  
  31.         return referenceDate ;  
  32.     }  
  33.       
  34.     @Override  
  35.     protected ModelAndView onSubmit(Object command) throws Exception{  
  36.         User user = (User)command ;  
  37.         userService.add(user) ;  
  38.         return new ModelAndView("userSuccess","user",user) ;  
  39.     }  
  40. }  

这儿我们需要在UserService和UserServiceImpl里增加相应的方法,设置相应的值。

[html]  view plain copy
  1. package com.zcl.spring.formtag;  
  2.   
  3. import java.util.List;  
  4.   
  5. public interface UserService {  
  6.     public void add(User user) ;  
  7.     public List<Country> getAllCountries();  
  8.     public List<Community> getAllCommunities();  
  9. }  

[java]  view plain copy
  1. package com.zcl.spring.formtag;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class UserServiceImpl implements UserService {  
  7.   
  8.     @Override  
  9.     public void add(User user) {  
  10.         System.out.println("User added successfully") ;  
  11.     }  
  12.     @SuppressWarnings("unchecked")  
  13.     @Override  
  14.     public List getAllCountries() {  
  15.         List countryList = new ArrayList();  
  16.         countryList.add(new Country(1,"India"));  
  17.         countryList.add(new Country(2,"USA"));  
  18.         countryList.add(new Country(3,"UK"));  
  19.         return countryList;  
  20.     }  
  21.   
  22.     @SuppressWarnings("unchecked")  
  23.     @Override  
  24.     public List getAllCommunities() {  
  25.         List communityList = new ArrayList();  
  26.         communityList.add(new Community("Spring","Spring"));  
  27.         communityList.add(new Community("Hibernate","Hibernate"));  
  28.         communityList.add(new Community("Struts","Struts"));  
  29.         return communityList;  
  30.     }  
  31. }   

配置dispatcher-servlet.xml文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xsi:schemaLocation="http://www.springframework.org/schema/beans   
  3. http://www.springframework.org/schema/beans/spring-beans.xsd  
  4. http://www.springframework.org/schema/context  
  5. http://www.springframework.org/schema/context/spring-context.xsd"  
  6. xmlns:context="http://www.springframework.org/schema/context"   
  7. xmlns:p="http://www.springframework.org/schema/p"   
  8. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  9. xmlns="http://www.springframework.org/schema/beans">   
  10.     <bean id="viewResolver"  
  11.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  12.         <property name="prefix">  
  13.             <value>/WEB-INF/jsp/</value>  
  14.         </property>  
  15.         <property name="suffix">  
  16.             <value>.jsp</value>  
  17.         </property>  
  18.     </bean>  
  19.     <bean id="userService" class="com.zcl.spring.formtag.UserServiceImpl" />  
  20.     <!-- <context:component-scan base-package="com.zcl.spring.simpleform" />  
  21.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
  22.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />-->  
  23.     <bean class="com.zcl.spring.formtag.UserController" p:successView="userSuccess" p:formView="userForm" p:userService-ref="userService" name="/userRegistration.html"/>  
  24. </beans>  

修改显示页面,使用jstl标签。下载jstl包将c.tld拷贝到WEB-INF下,然后就可以直接使用了,在jsp文件里加上<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>表示支持他的核心标签库。

[html]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  8. <title>Success page</title>  
  9. </head>  
  10. <body>  
  11.         User Details  
  12.         <hr>  
  13.     User Name   : <c:out value="${user.name}"></c:out> <br />  
  14.     Gender      : <c:out value="${user.gender}"></c:out> <br />  
  15.     Country     : <c:out value="${user.country}"></c:out> <br />  
  16.     About You   : <c:out value="${user.aboutYou}"></c:out> <br />  
  17.     Community   : <c:forEach var="community" items="${user.communityList}">  
  18.                     <c:out value="${community}"></c:out>  
  19.                  </c:forEach> <br />  
  20.     Mailing List: <c:out value="${user.mailingList} "></c:out>  
  21. </body>  
  22. </html>  

运行结果和前面一样,注意要通过首页“response.sendRedirect("userRegistration.html"); ”跳转过去。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值