@RequestMapping中value参数的值怎么能从注释的方法中获取json中的value值?

没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!博客分类:
一、SpringMVC注解入门
1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器
&!-- mvc的注解驱动 --&
&mvc:annotation-driven /&
&!--只要定义了扫描器,注解驱动就不需要,扫描器已经有了注解驱动的功能 --&
&context:component-scan base-package="org.study1.mvc.controller" /&
&!-- 前缀+ viewName +后缀 --&
class="org.springframework.web.servlet.view.InternalResourceViewResolver"&
&!-- WebContent(WebRoot)到某一指定的文件夹的路径 ,如下表示/WEB-INF/view/*.jsp --&
&property name="prefix" value="/WEB-INF/view/"&&/property&
&!-- 视图名称的后缀 --&
&property name="suffix" value=".jsp"&&/property&
&context:component-scan/& 扫描指定的包中的类上的注解,常用的注解有:
@Controller 声明Action组件@Service
声明Service组件
@Service("myMovieLister") @Repository 声明Dao组件@Component
泛指组件, 当不好归类时. @RequestMapping("/menu")
请求映射@Resource
用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName") @Autowired 用于注入,(srping提供的) 默认按类型装配 @Transactional( rollbackFor={Exception.class}) 事务管理@ResponseBody@Scope("prototype")
设定bean的作用
3. @controller:标识当前类是控制层的一个具体的实现4. @requestMapping:放在方法上面用来指定某个方法的路径,当它放在类上的时候相当于命名空间需要组合方法上的requestmapping来访问。
@Controller // 用来标注当前类是springmvc的控制层的类
@RequestMapping("/test") // RequestMapping表示 该控制器的唯一标识或者命名空间
public class TestController {
* 方法的返回值是ModelAndView中的
@RequestMapping("/hello.do") // 用来访问控制层的方法的注解
public String hello() {
System.out.println("springmvc annotation... ");
return "jsp1/index";
在本例中,项目部署名为mvc,tomcat url为 ,所以实际为:
在本例中,因为有命名空间 /test,所以请求hello方法地址为:http://localhost/mvc/test/hello.do
输出:springmvc annotation...
二、注解形式的参数接收
1. HttpServletRequest可以直接定义在参数的列表,通过该请求可以传递参数
url:http://localhost/mvc/test/toPerson.do?name=zhangsan
* HttpServletRequest可以直接定义在参数的列表,
@RequestMapping("/toPerson.do")
public String toPerson(HttpServletRequest request) {
String result = request.getParameter("name");
System.out.println(result);
return "jsp1/index";
可以从HttpServletRequest 取出“name”属性,然后进行操作!如上,可以取出 “name=zhangsan”
输出:zhangsan2. 在参数列表上直接定义要接收的参数名称,只要参数名称能匹配的上就能接收所传过来的数据, 可以自动转换成参数列表里面的类型,注意的是值与类型之间是可以转换的
2.1传递多种不同类型的参数:
url:http://localhost/mvc/test/toPerson1.do?name=zhangsan&age=14&address=china&birthday=
* 传递的参数的名字必须要与实体类的属性set方法后面的字符串匹配的上才能接收到参数,首字符的大小写不区分
* 请求中传的参数只要是能和参数列表里面的变量名或者实体里面的set后面的字符串匹配的上就能接收到 a
@RequestMapping("/toPerson1.do")
public String toPerson1(String name, Integer age, String address,
Date birthday) {
System.out.println(name + " " + age + " " + address + " " + birthday);
return "jsp1/index";
* 注册时间类型的属性编辑器,将String转化为Date
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true));
输出:zhangsan 14 china Fri Feb 11 00:00:00 CST 2000
2.2传递数组:
url:http://localhost/mvc/test/toPerson2.do?name=tom&name=jack
* 对数组的接收,定义为同名即可
@RequestMapping("/toPerson2.do")
public String toPerson2(String[] name) {
for (String result : name) {
System.out.println(result);
return "jsp1/index";
输出:tom jack
2.3传递自定义对象(可多个):
url:http://localhost/mvc/test/toPerson3.do?name=zhangsan&age=14&address=china&birthday=
User 定义的属性有:name,age,并且有各自属性的对应的set方法以及toString方法
Person定义的属性有:name,age.address,birthday,并且有各自属性的对应的set方法以及toString方法
* 传递的参数的名字必须要与实体类的属性set方法后面的字符串匹配的上才能接收到参数,首字符的大小写不区分
* 请求中传的参数只要是能和参数列表里面的变量名或者实体里面的set后面的字符串匹配的上就能接收到
@RequestMapping("/toPerson3.do")
public String toPerson3(Person person, User user) {
System.out.println(person);
System.out.println(user);
return "jsp1/index";
Person [name=zhangsan, age=14, address=china, birthday=Fri Feb 11 00:00:00 CST 2000]User [name=zhangsan, age=14]
自动封装了对象,并且被分别注入进来!
三、注解形式的结果返回
1. 数据写到页面,方法的返回值采用ModelAndView, new ModelAndView("index", map);,相当于把结果数据放到response里面
url:http://localhost/mvc/test/toPerson41.do
url:http://localhost/mvc/test/toPerson42.do
url:http://localhost/mvc/test/toPerson43.do
url:http://localhost/mvc/test/toPerson44.do
* HttpServletRequest可以直接定义在参数的列表,并且带回返回结果
@RequestMapping("/toPerson41.do")
public String toPerson41(HttpServletRequest request) throws Exception {
request.setAttribute("p", newPesion());
return "index";
* 方法的返回值采用ModelAndView, new ModelAndView("index", map);
* ,相当于把结果数据放到Request里面,不建议使用
@RequestMapping("/toPerson42.do")
public ModelAndView toPerson42() throws Exception {
Map&String, Object& map = new HashMap&String, Object&();
map.put("p", newPesion());
return new ModelAndView("index", map);
* 直接在方法的参数列表中来定义Map,这个Map即使ModelAndView里面的Map,
* 由视图解析器统一处理,统一走ModelAndView的接口,也不建议使用
@RequestMapping("/toPerson43.do")
public String toPerson43(Map&String, Object& map) throws Exception {
map.put("p", newPesion());
return "index";
* 在参数列表中直接定义Model,model.addAttribute("p", person);
* 把参数值放到request类里面去,建议使用
@RequestMapping("/toPerson44.do")
public String toPerson44(Model model) throws Exception {
// 把参数值放到request类里面去
model.addAttribute("p", newPesion());
return "index";
* 为了测试,创建一个Persion对象
public Person newPesion(){
Person person = new Person();
person.setName("james");
person.setAge(29);
person.setAddress("maami");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("");
person.setBirthday(date);
以上四种方式均能达到相同的效果,但在参数列表中直接定义Model,model.addAttribute("p", person);把参数值放到request类里面去,建议使用
2. Ajax调用springmvc的方法:直接在参数的列表上定义PrintWriter,out.write(result);把结果写到页面,建议使用的
url:http://localhost/mvc/test/toAjax.do
* ajax的请求返回值类型应该是void,参数列表里直接定义HttpServletResponse,
* 获得PrintWriter的类,最后可把结果写到页面 不建议使用
@RequestMapping("/ajax1.do")
public void ajax1(String name, HttpServletResponse response) {
String result = "hello " +
response.getWriter().write(result);
} catch (IOException e) {
e.printStackTrace();
* 直接在参数的列表上定义PrintWriter,out.write(result);
* 把结果写到页面,建议使用的
@RequestMapping("/ajax2.do")
public void ajax2(String name, PrintWriter out) {
String result = "hello " +
out.write(result);
* 转向ajax.jsp页面
@RequestMapping("/toAjax.do")
public String toAjax() {
return "ajax";
ajax页面代码如下:
&script type="text/javascript" src="js/jquery-1.6.2.js"&&/script&
&script type="text/javascript"&
$(function(){
$("#mybutton").click(function(){
url:"test/ajax1.do",
type:"post",
dataType:"text",
name:"zhangsan"
success:function(responseText){
alert(responseText);
error:function(){
alert("system error");
&input id="mybutton" type="button" value="click"&
四、表单提交和重定向
1、表单提交:
请求方式的指定:@RequestMapping( method=RequestMethod.POST )可以指定请求方式,前台页面就必须要以它制定好的方式来访问,否则出现405错误
表单jsp页面:
&base href="&%=basePath%&"&
&title&SpringMVC Form&/title&
&form action="test/toPerson5.do" method="post"&
name:&input name="name" type="text"&&br&
age:&input name="age" type="text"&&br&
address:&input name="address" type="text"&&br&
birthday:&input name="birthday" type="text"&&br&
&input type="submit" value="submit"&&br&
对应方法为:
* 转向form.jsp页面
@RequestMapping("/toform.do")
public String toForm() {
return "form";
* @RequestMapping( method=RequestMethod.POST)
* 可以指定请求方式,前台页面就必须要以它制定好的方式来访问,否则出现405错误 a
@RequestMapping(value = "/toPerson5.do", method = RequestMethod.POST)
public String toPerson5(Person person) {
System.out.println(person);
return "jsp1/index";
2. 重定向:controller内部重定向,redirect:加上同一个controller中的requestMapping的值,controller之间的重定向:必须要指定好controller的命名空间再指定requestMapping的值,redirect:后必须要加/,是从根目录开始
* controller内部重定向
* redirect:加上同一个controller中的requestMapping的值
@RequestMapping("/redirectToForm.do")
public String redirectToForm() {
return "redirect:toform.do";
* controller之间的重定向:必须要指定好controller的命名空间再指定requestMapping的值,
* redirect:后必须要加/,是从根目录开始
@RequestMapping("/redirectToForm1.do")
public String redirectToForm1() {
//test1表示另一个Controller的命名空间
return "redirect:/test1/toForm.do";
参考资料:
浏览 52490
浏览: 115284 次
来自: 小城市
[color=orange][/color][/size]l] ...
写的还好。
与节省内存空间
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'获取Spring MVC中所有RequestMapping以及对应方法和参数 - 简书
获取Spring MVC中所有RequestMapping以及对应方法和参数
在Spring MVC中想要对每一个URL进行权限控制,不想手工整理这样会有遗漏,所以就动手写程序了。代码如下:
* @author Elwin ZHANG
* 创建时间:日 上午11:48:22
* 功能:返回系统中的所有控制器映射路径,以及对应的方法
@RequestMapping(value = "/maps", produces = "application/ charset=utf-8")
@ResponseBody
public Object getMapPaths(){
String result="";
RequestMappingHandlerMapping rmhp = springHelper.getObject(RequestMappingHandlerMapping.class);
Map&RequestMappingInfo, HandlerMethod& map = rmhp.getHandlerMethods();
for(RequestMappingInfo info : map.keySet()){
result +=info.getPatternsCondition().toString().replace("[", "").replace("]", "")+ "\t"
HandlerMethod
hm=map.get(info);
result +=hm.getBeanType().getName()+ "\t"
result +=getMethodParams(hm.getBeanType().getName(),hm.getMethod().getName())+ "\t";
result +=info.getProducesCondition().toString().replace("[", "").replace("]", "")+ "\t"
result += "\r\n";
getMethodParams是专门用于获取方法中参数名称的函数,因为用Java自身的反射功能是获取不到的,浪费我不少时间,后来网上看到JBOSS的JAVAssist类可以。其实这个JAVAssist类库也被封装在Mybatis中,如果系统使用了Mybatis,则直接引入可以使用了。
import org.apache.ibatis.javassist.*;
import org.apache.ibatis.javassist.bytecode.*;
getMethodParams 的实现如下:
* @param className 类名
* @param methodName 方法名
该方法的声明部分
* @author Elwin ZHANG
* 创建时间:日 上午11:47:16
* 功能:返回一个方法的声明部分,包括参数类型和参数名
private String getMethodParams(String className,String methodName){
String result="";
ClassPool pool=ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(this.getClass());
pool.insertClassPath(classPath);
CtMethod cm =pool.getMethod(className, methodName);
// 使用javaassist的反射方法获取方法的参数名
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
result=cm.getName() + "(";
if (attr == null) {
return result + ")";
pTypes=cm.getParameterTypes();
String[] paramNames = new String[pTypes.length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i & paramNames. i++)
if(!pTypes[i].getSimpleName().startsWith("HttpServletRe")){
result += pTypes[i].getSimpleName();
paramNames[i] = attr.variableName(i + pos);
result += " " + paramNames[i]+",";
if(result.endsWith(",")){
result=result.substring(0, result.length()-1);
result+=")";
}catch(Exception e){
e.printStackTrace();
这样就可以获得每个URL路径与期对应的方法声明了。
另外SpringHelper是自己封装的Spring工具类,可以用来直接获取Spring管理的Bean,代码如下:
import java.util.L
import javax.servlet.http.HttpServletR
import org.apache.log4j.L
import org.springframework.beans.BeansE
import org.springframework.beans.factory.annotation.A
import org.springframework.context.ApplicationC
import org.springframework.context.ApplicationContextA
import org.springframework.stereotype.C
import org.springframework.web.servlet.i18n.CookieLocaleR
* @author Elwin ZHANG
* 创建时间:日 上午9:12:13
* 功能:Spring 工具类,用于获取Spring管理的Bean
@Component
public class SpringHelper implements ApplicationContextAware {
// 日志输出类
private static Logger logger = Logger.getLogger(SpringHelper.class);
// 当前的Spring上下文
private static ApplicationContext applicationC
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
applicationContext = arg0;
* @param beanName bean Id
如果获取失败,则返回Null
* @author Elwin ZHANG
* 创建时间:日 上午9:52:55
* 功能:通过BeanId获取Spring管理的对象
Object getObject(String beanName) {
Object object =
object = applicationContext.getBean(beanName);
} catch (Exception e) {
logger.error(e);
* @author Elwin ZHANG
* 创建时间:日 下午3:44:38
* 功能:获取Spring的ApplicationContext
public ApplicationContext
getContext() {
return applicationC
* @param clazz 要获取的Bean类
如果获取失败,则返回Null
* @author Elwin ZHANG
* 创建时间:日 上午10:05:27
* 功能:通过类获取Spring管理的对象
&T& T getObject(Class&T& clazz) {
return applicationContext.getBean(clazz);
} catch (Exception e) {
logger.error(e);
* @param code 配置文件中消息提示的代码
* @param locale 当前的语言环境
* @return 当前语言对应的消息内容
* @author Elwin ZHANG
* 创建时间:日 上午10:34:25
* 功能:获取当前语言对应的消息内容
String getMessage(String code,Locale locale){
message=applicationContext.getMessage(code, null, locale);
}catch(Exception e){
logger.error(e);
message="";
* @param code 配置文件中消息提示的代码
* @param request 当前的HTTP请求
* @return 当前语言对应的消息内容
* @author Elwin ZHANG
* 创建时间:日 下午3:03:37
* 功能:获取当前语言对应的消息内容
String getMessage(String code,HttpServletRequest request){
message=applicationContext.getMessage(code, null, getCurrentLocale(request));
}catch(Exception e){
logger.error(e);
message="zh_CN";
* @param request 当前的HTTP请求
* @return 当前用户Cookie中的语言
* @author Elwin ZHANG
* 创建时间:日 下午2:59:21
* 功能:当前用户保存Cookie中的默认语言
Locale getCurrentLocale(HttpServletRequest request){
return resolver.resolveLocale(request);
//Cookie本地语言解析器,Spring提供
@Autowired
CookieLocaleR
我是一棵秋天的树
Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgbook/spring-boot-reference-guide-zh/details带目录浏览地址:http://www.maoyupeng.com/sprin...
Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)。分布式系统的协调导致了样板模式, 使用Spring Cloud开发人员可以快速地支持实现这些模式的服务和应用程序。他们将在任何分布式...
百战程序员_ Java1573题 QQ群:034603 掌握80%年薪20万掌握50%年薪10万 全程项目穿插, 从易到难,含17个项目视频和资料持续更新,请关注www.itbaizhan.com 国内最牛七星级团队马士兵、高淇等11位十年开发经验专...
1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接...
1.Spring整体架构 (1)Core container 核心容器包括了Core、Beans、Context和Expression Language模块。 Core和Beans模块是框架的基础部分,提供了IoC和DI特性,这里的基础概念是BeanFactory,它提供对...
风铎老师《夫妻关系中,你了解另一半的需求吗》微课回顾 文字整理:海燕 恋爱的激情过后,走进婚姻的两个人往往会发觉,对方似乎再也不像热恋期那么懂你。 你了解你的伴侣吗? 你能很好的捕捉到伴侣的需求吗? 懂他/她,才能更好的去爱他/她。 了解他/她真正的需求,夫妻之间才能拥有更...
当肩膀挑起了一群人的性命 你不过是个悲伤的躯壳 只有苦楚,三月再也没上山 六月也不会下水,不忘了打伞 现在你拳头攥着匕首 那本该是一朵花或一瓶酒 酒依然常喝,但香烟萦绕 从故事和读书时变得深刻 爱讲浪子和和尚 而摆在面前的是装金钵 往回的马垂着尾巴 将耳边的铃铛扔进被窝 穿...
在儿子最后的一个“六一”儿童节,相约老师,相约同学,相约朋友,相约父母亲,以孩子们喜欢的方式,放飞青春梦想的活动来一场告别仪式。
要说近期最“万万没想到”的事—— 应该是#墨镜王#、#梁朝伟#这两大金牌又撞车了,还整了部贺岁喜剧片。 预告一出来,网友一致表示“不约”。 无凭无据这么说…… Sir猜测,可能是因为预告片里王晶式一锅乱炖的既视感。 但这也暴露出一个不容忽视的事实—— 梁朝伟主演的好喜剧,已...没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!springmvc中@requestmapping 方法中怎么直接收到客户端穿过来的参数值_百度知道
springmvc中@requestmapping 方法中怎么直接收到客户端穿过来的参数值
我有更好的答案
对于Struts 如何控制、处理客户请求,让我们通过对struts的四个核心组件介绍来具体说明。这几个组件就是:ActionServlet。Action Classes,Action Mapping(此处包括ActionForward),ActionFrom Bean。   二、Spring   Spring实际上是《Expert One-on-One J2EE Design and Development》一书中所阐述的设计思想的具体实现。在One-on-One一书中,Rod Johnson 倡导J2EE 实用主义的设计思想,并随书提供了一个初步的开发框架实现(interface21 开发包)。而Spring 正是这一思想的更全面和具体的体现。Rod Johnson在interface21 开发包的基础之上,进行了进一步的改造和扩充,使其发展为一个更加开放、清晰、全面、高效的开发框架。   Spring是一个开源框架,由Rod Johnson创建并且在他的著作《J2EE设计开发编程指南》里进行了描述。它是为了解决企业应用开发的复杂性而创建的。Spring使使用基本的JavaBeans来完成以前只可能由EJB完成的事情变得可能了。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。   简单来说,Spring是一个轻量的控制反转和面向切面的容器框架。当然,这个描述有点过于简单。但它的确概括出了Spring是做什么的。为了更好地理解Spring,让我们分析一下这个描述:   1、轻量   从大小与开销两方面而言Spring都是轻量的。完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。并且Spring所需的处理开销也是微不足道的。此外,Spring是非侵入式的:典型地,Spring应用中的对象不依赖于轻量,从大小与开销两方面而言Spring都是轻量的。完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。并且Spring所需的处理开销也是微不足道的。此外,Spring是非侵入式的:典型地,Spring应用中的对象不依赖于Spring的特定类。   2、控制反转   Spring通过一种称作控制反转(IoC)的技术促进了松耦合。当应用了IoC,对象被动地传递它们的依赖而不是自己创建或者查找依赖对象。你可以认为IoC与JNDI相反不是对象从容器中查找依赖,而是容器在对象初始化时不等被请求就将依赖传递给它。   3、面向切面   Spring包含对面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统服务(例如审计与事物管理)进行内聚性的开发。应用对象只做它们应该做的,完成业务逻辑,仅此而已。它们并不负责(甚至是意识)其它的系统关注点,例如日志或事物支持。   4、容器   Spring包含和管理应用对象的配置和生命周期,在这个意义上它是一种容器。你可以配置你的每个bean如何被创建基于一个配置原形为你的bean创建一个单独的实例或者每次需要时都生成一个新的实例以及它们是如何相互关联的。然而,Spring不应该被混同于传统的重量的EJB容器,它们经常是庞大与笨重的,难以使用。   框架:Spring是由简单的组件配置和组合复杂的应用成为可能。在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。Spring也提供了很多基础功能(事务管理、持久性框架集成等等),将应用逻辑的开发留给了你。   所有Spring的这些特征使你能够编写更干净、更可管理、并且更易于测试的代码。它们也为Spring中的各种子框架提供了基础。
采纳率:75%
为您推荐:
其他类似问题
参数值的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 向input中追加value值 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信