南昌建站费用,江宁做网站,秦皇岛市人事考试网,wordpress 除了一、RequestMaapping的基本介绍 RequestMaapping的功能就是将请求和处理请求和处理请求的控制器关联起来#xff0c;建立映射关系#xff0c;当DispathcerServlet接收到请求#xff0c;会从Controller中找对应的方法来处理该请求。
eg:
Controller
RequestMapping(/…一、RequestMaapping的基本介绍 RequestMaapping的功能就是将请求和处理请求和处理请求的控制器关联起来建立映射关系当DispathcerServlet接收到请求会从Controller中找对应的方法来处理该请求。
eg:
Controller
RequestMapping(/test)
public class TestRequestMappingController {RequestMapping(/hello)public String hello(){return success;}
}
当浏览器中的请求是url/test此时服务器端通过dispatcherservlet处理之后从此项目的配置文件中寻找控制器中与之对应的路径。
1、位置
可以放在类上也可以放在方法上 放在类上声明就是请求路径的初始信息 放在方法上声明就是请求路径的具体信息
比如上面的例子当想要实现hello页面的时候具体的路径应为url/test/hello而不是url/hello。 a th:href{/test/hello}测试RequestMapping/abr/
2、属性 该注解中还有几个属性这里只说说value、method Value 其就是通过value属性的值匹配请求地址中的url。相当于 RequestMapping(value {/testRequestMapping, /test}) method 与请求中的请求方式进行匹配当满足时才可以调用对应的方法。 RequestMapping( value {/testRequestMapping, /test}, method {RequestMethod.GET, RequestMethod.POST} ) 3、占位符 之后在SpringMVC中路径都是以RestFul的形式发送原始方式/deleteUser?id1 rest方式/user/delete/1 即就是不通过“”来作为路径和属性了分割的标志了。所以对应的映射需要重新设置为如下格式 通过PathVariable注解将请求中的数据赋给形参了。 a th:href{/testRest/1/admin}测试路径中的占位符--/testRest/abr RequestMapping(/testRest/{id}/{username})
public String testRest(PathVariable(id) String id,PathVariable(username) String username)
{ System.out.println(id:id,username:username); return success; } 当我们想要从一个页面上直接访问到一个特定的信息中可以用此方法。 比如QQ空间这类项目在我们的空间中想要访问到指定好友的空间就需要点击一些图片类的超链接然而超链接中就有这些数据我们只需要想办法接收即可。 二、获取请求参数 1、通过ServletAPI获取老方法 form th:action{/param/servletAPI} methodget用户名:input typetext nameusernamebr/密码:input typepassword namepasswordbr/input typesubmit value登录br//form RequestMapping(/param/servletAPI)public String getParamByServletAPI(HttpServletRequest request){String username request.getParameter(username);String password request.getParameter(password);System.out.println(username:username,password:password);return success;} 2、通过控制器方法的形参直接获取 RequestMapping(/param)public String getParam(RequestParam(value userName,required false,defaultValue hello) String username, String password){System.out.println(username:username,password:password);return success;} 最简单的时候我们都不需要设置RequestParam这个注释来处理。只将控制器方法中的i形参和发送过来数据的name一致就能匹配上。 RequestParam是为了处理方法中的形参和name值不一致的时候我们手动设置。 value请求中的name具体指要将哪个值赋予方法中的形参。 Required指是否需要有值当为true的时候若没有值传过来就会报错。 dafalueValue:是指当没有对应的值在请求中时我们赋予的默认参数不管required的值时啥。只要没有值就默认赋值。 3、通过pojo类获取 这个很简单只需要请求中的参数和参数名一一匹配即可。 RequestMapping(/param/pojo)public String getParamByPojo(User user){System.out.println(user);return success;} 4、处理乱码的问题 需要有一个认知最初只有serlvet的时候我们都是通过Servlet的APIcharactersetEncoding这段代码放在代码的最初来设置字符类型。但在SpringMVC下我们的dispatcherServlet会处理所有的请求然后在通过配置文件扫描到我们对应的控制器中然后匹配对应的方法。所以请求信息中有参数的时候如果我们在方法里设置字符类型已经不起作用了因为已经接收到了就差赋值给形参这一步了。所以我们需要在web.xml配置文件中早早的设置这个字符类型。 通过过滤器的方式来设置的 filterfilter-nameCharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-param!--不但会设置请求的编码类型也会设置响应的编码类型--init-paramparam-nameforceEncoding/param-nameparam-valuetrue/param-value/init-param/filterfilter-mappingfilter-nameCharacterEncodingFilter/filter-nameurl-pattern//url-pattern/filter-mapping 三、域对象共享数据 现在只需要处理三个域类型即可把page删了。 1、请求域 方法一通过ServletAPI RequestMapping(/testServletAPI)
public String testServletAPI(HttpServletRequest request)
{ request.setAttribute(testScope, hello,servletAPI);return success; } 方法二使用ModelAndView也是SpringMVC底层的代码 RequestMapping(/test/mav)public ModelAndView testMAV(){/** modelAndView 包含model和view功能* model向请求域中共享数据* view:设置逻辑视图实现页面跳转* */ModelAndView mav new ModelAndView();//向请求域中共享数据mav.addObject(testRequestScope,hello.modelandView);//设置逻辑视图mav.setViewName(success);return mav;} 方法三使用Model其实也是方法二的子类或者是实现类 RequestMapping(/test/model)public String testModel(Model model){model.addAttribute(testRequestScope,hello,model);return success;} 2、Session和Application 这两个一样都是通过ServletAPI中的方法即可老师讲说这个最简便 RequestMapping(/testSession)
public String testSession(HttpSession session)
{ session.setAttribute(testSessionScope, hello,session); return success; }RequestMapping(/testApplication)
public String testApplication(HttpSession session)
{
ServletContext application session.getServletContext(); application.setAttribute(testApplicationScope, hello,application);return success; }