Spring表单标签和modelAttribute

  1. <span style=“color:rgb(255,0,0);font-family:Helvetica, Tahoma, Arial, sans-serif;line-height:18px;”>web.xml</span>  
web.xml
  1. <?xml version=“1.0” encoding=“UTF-8”?>   
  2. <web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns=“http://java.sun.com/xml/ns/javaee” xmlns:web=“http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” version=“2.5”>   
  3. <context-param>   
  4.     <param-name>contextConfigLocation</param-name>   
  5.     <param-value>classpath*:/springMVC.xml</param-value>   
  6. </context-param>   
  7. <listener>   
  8.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  9.   </listener>   
  10.   
  11.   <servlet>   
  12.     <servlet-name>springMVC</servlet-name>   
  13.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   
  14.     <init-param>   
  15.       <param-name>contextConfigLocation</param-name>   
  16.       <param-value>classpath*:/springMVC.xml</param-value>   
  17.     </init-param>   
  18.     <load-on-startup>1</load-on-startup>   
  19.   </servlet>   
  20.   <servlet-mapping>   
  21.     <servlet-name>springMVC</servlet-name>   
  22.     <url-pattern>/</url-pattern>   
  23.   </servlet-mapping>   
  24.   <welcome-file-list>   
  25.     <welcome-file>index.jsp</welcome-file>   
  26.   </welcome-file-list>   
  27. </web-app>   
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:/springMVC.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener> 

  <servlet> 
    <servlet-name>springMVC</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>classpath*:/springMVC.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>springMVC</servlet-name> 
    <url-pattern>/</url-pattern> 
  </servlet-mapping> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

springMVC.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>   
  2. <beans xmlns=“http://www.springframework.org/schema/beans”   
  3. xmlns:context=“http://www.springframework.org/schema/context”   
  4. xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”   
  5. xmlns:mvc=“http://www.springframework.org/schema/mvc”   
  6. xsi:schemaLocation=”http://www.springframework.org/schema/mvc   
  7. http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   
  8.         http://www.springframework.org/schema/context   
  9.         http://www.springframework.org/schema/context/spring-context-3.1.xsd   
  10. http://www.springframework.org/schema/beans   
  11. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd”>   
  12.   
  13. <mvc:resources mapping=“/resources/**” location=“/”/>   
  14. <mvc:annotation-driven/>   
  15. <context:component-scan base-package=“com.test.controller” />   
  16. <bean  class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>   
  17. <property name=“prefix”><value>/WEB-INF/pages/</value></property>   
  18. <property name=“suffix”><value>.jsp</value></property>   
  19. </bean>   
  20. </beans>   
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 

<mvc:resources mapping="/resources/**" location="/"/> 
<mvc:annotation-driven/> 
<context:component-scan base-package="com.test.controller" /> 
<bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix"><value>/WEB-INF/pages/</value></property> 
<property name="suffix"><value>.jsp</value></property> 
</bean> 
</beans> 
/创建一个实体Book类 

  1. public class Book {   
  2. String name;   
  3. String press;   
  4. public String getName() {   
  5. return name;   
  6. }   
  7. public void setName(String name) {   
  8. this.name = name;   
  9. }   
  10. public String getPress() {   
  11. return press;   
  12. }   
  13. public void setPress(String press) {   
  14. this.press = press;   
  15. }   
  16.   
  17. }   
public class Book { 
String name; 
String press; 
public String getName() { 
return name; 
} 
public void setName(String name) { 
this.name = name; 
} 
public String getPress() { 
return press; 
} 
public void setPress(String press) { 
this.press = press; 
} 

} 
  1. @Controller   
  2. @RequestMapping(“/welcome”)   
  3. public class Hello {   
  4.    //处理index.jsp表单提交   
  5.    @RequestMapping(value=“/show.do”,method=RequestMethod.POST)   
  6.    public String show(@ModelAttribute(“book”)Book book,Map<String,Object>model){   
  7. model.put(”book”,book);   
  8. return “hello”;   
  9. }   
  10. //我们不能直接访问使用Spring表单标签的JSP页面,要先通过controller处理modelAttribute和表单标签的绑定才能访问   
  11.           @RequestMapping(value=“/new.do”,method=RequestMethod.GET)   
  12. public String login(Map model){   
  13. Book book=new Book();          
  14. model.put(”book”,book);   
  15. return “index”;   
  16. }   
  17. }   
@Controller 
@RequestMapping("/welcome") 
public class Hello { 
   //处理index.jsp表单提交 
   @RequestMapping(value="/show.do",method=RequestMethod.POST) 
   public String show(@ModelAttribute("book")Book book,Map<String,Object>model){ 
model.put("book",book); 
return "hello"; 
} 
//我们不能直接访问使用Spring表单标签的JSP页面,要先通过controller处理modelAttribute和表单标签的绑定才能访问 
          @RequestMapping(value="/new.do",method=RequestMethod.GET) 
public String login(Map model){ 
Book book=new Book();        
model.put("book",book); 
return "index"; 
} 
} 
通过controller处理后,book里的属性自动和表单标签里的”path”绑定,简单来说,其实path就是起到了book.getXXX()的作用。 
      切记,path后跟着的名字要与对象的属性名字一样,否则绑定不了  

index.jsp 

  1. <%@ page language=“java” contentType=“text/html; charset=UTF-8”   
  2.     pageEncoding=“UTF-8”%>   
  3. <%@ taglib prefix=“form” uri=“http://www.springframework.org/tags/form”%>   
  4. <%@ page import=“com.test.entity.*” %>   
  5. <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>   
  6. <html>   
  7. <head>   
  8. <meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>   
  9. <title>Insert title here</title>   
  10. </head>   
  11. <body>   
  12. <form:form modelAttribute=“book” method=“POST” action=“show.do”>   
  13. <table>   
  14. <tr>   
  15. <td><form:input path=“name”/></td>   
  16. </tr>   
  17. <tr>   
  18. <td><form:input path=“press”/></td>   
  19. </tr>   
  20. <tr>   
  21. <td><input type=“submit” value=“O K”> </td>   
  22. </tr>   
  23. </table>   
  24. </form:form>   
  25. </body>   
  26. </html>   
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<%@ page import="com.test.entity.*" %> 
<!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=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<form:form modelAttribute="book" method="POST" action="show.do"> 
<table> 
<tr> 
<td><form:input path="name"/></td> 
</tr> 
<tr> 
<td><form:input path="press"/></td> 
</tr> 
<tr> 
<td><input type="submit" value="O K"> </td> 
</tr> 
</table> 
</form:form> 
</body> 
</html> 
hello.jsp
  1. <%@ page language=“java” contentType=“text/html; charset=UTF-8”   
  2.     pageEncoding=“utf-8”%>   
  3. <%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>   
  4.   
  5. <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>   
  6. <html>   
  7. <head>   
  8. <meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”>   
  9. <title>Insert title here</title>   
  10. </head>   
  11. <body>   
  12. bookName= book.getName(),</span><spanclass="attribute">press</span><span>= b o o k . g e t N a m e ( ) , < / s p a n >< s p a n c l a s s =" a t t r i b u t e "> p r e s s < / s p a n >< s p a n >= {book.getPress()}   
  13. </body>   
  14. </html>   
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="utf-8"%> 
<%@ 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>Insert title here</title> 
</head> 
<body> 
bookName=${book.getName()},press=${book.getPress()} 
</body> 
</html> 





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值