spring @RequestParam注释

这个注释可以从请求中获得参数。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值