继续学习SpringMVC,JSON和Param是两种常用的传值的方法,其中JSON是用的最为普遍的,而现在我们来学习一下另一种方法就是param
最终结果:
由图可见我们成功访问到了所输的数值,换一个数字照样行得通
简要实现:
package com.aurora.param; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("param") public class ParamController { // 直接接收 // /param/data?name=root&age=18 // 形参列表,填写对应名称的参数即可,请求参数名 = 形参参数名即可 // @RequestMapping("data") @ResponseBody public String data(String name,int age){ System.out.println("name = " + name + ", age = " + age); return "name = " + name + ", age = " + age; } }
package com.aurora.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class SpringMVCInit extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[0]; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{MvcConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{ "/" }; } }
package com.aurora.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; @Configuration @ComponentScan("com.aurora.param") public class MvcConfig { @Bean public RequestMappingHandlerMapping handlerMapping(){ return new RequestMappingHandlerMapping(); } @Bean public RequestMappingHandlerAdapter handlerAdapter(){ return new RequestMappingHandlerAdapter(); } }
标签:return,SpringMVC,springframework,参数,param,org,import,annotation From: https://www.cnblogs.com/rsy-bxf150/p/17787751.html