close
Spging mvc 可以針對簡單的form去處理(結合JSTL)。
web.xml
<display-name>Spring MVC Form Handling</display-name>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring-config.xml
<context:component-scan base-package="com.example" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Customer.java
package com.example; public class Customer { private Integer age ; private String name ; private String address ; public Integer getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
CustomerControler.java
package com.example; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class CustomerControler { @RequestMapping(value = "/customer", method = RequestMethod.GET) public ModelAndView student() { return new ModelAndView("customer", "command", new Customer()); } @RequestMapping(value="/addCustomer", method = RequestMethod.POST) public String addCustomer(@ModelAttribute("HelloSpring") Customer cust,ModelMap model) { model.addAttribute("age", cust.getAge()); model.addAttribute("name", cust.getName()); model.addAttribute("address", cust.getAddress()); return "showResult"; } }
customer.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>customer form information</title>
</head>
<body>
<form:form method="POST" action="/HelloSpring/addCustomer">
<table>
<tr>
<td><form:label path="age">age</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><form:label path="name">name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="address">address</form:label></td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
showResult.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>show result</title>
</head>
<body>
<table>
<tr>
<td>${age}</td>
</tr>
<tr>
<td>${name}</td>
</tr>
<tr>
<td>${address}</td>
</tr>
</table>
</body>
</html>
</pre>
全站熱搜
留言列表