Spring表单标签和modelAttribute

web.xml
<?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

<?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类 

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; 
} 

} 
@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 

<%@ 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
<%@ 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> 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值