这个注释可以从请求中获得参数。
package roseindia.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import roseindia.form.SampleForm;
@Controller
@RequestMapping(value = "/param/")
public class ApplicationController {
@RequestMapping(value = "/index")
public String loadIndex(Model model, SampleForm sampleForm) {
model.addAttribute("sampleForm", sampleForm);
return "index";
}
/*
* The method written below takes the request parameters individually by
* using @RequestParam However you can also use @ModelAttribute to take the
* form/model object.
*/
@RequestMapping(value = "/process-form")
public String processForm(
@RequestParam(value = "rollNo") Integer rollNo,
@RequestParam(value = "name") String name,
@RequestParam(value = "address") String address,
@RequestParam(value = "commonsMultipartFile") CommonsMultipartFile commonsMultipartFile,
Model model) {
model.addAttribute("rollNo", rollNo);
model.addAttribute("name", name);
model.addAttribute("address", address);
model.addAttribute("multipartFile", commonsMultipartFile);
return "view-data";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="roseindia" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>
index.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form action="process-form" commandName="sampleForm" method="POST"
enctype="multipart/form-data">
<table align="center">
<tr>
<td>Roll No</td>
<td><form:input path="rollNo" /></td>
</tr>
<tr>
<td>Name</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Address</td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td>Image</td>
<td><form:input type="file" path="commonsMultipartFile" /></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit"
value="Process" /></td>
</tr>
</table>
</form:form>
原文:
http://www.roseindia.net/tutorial/spring/request-param-annotation.html
源代码:http://pan.baidu.com/share/link?shareid=3761167212&uk=3878681452