Spring零散记
来源:互联网 发布:淘宝卖家如何打印订单 编辑:程序博客网 时间:2024/06/06 12:42
这里记录关于Spring的一些零散的知识点,以免自己日后忘记。
===============================
1.ModelAndView的addObject()方法.
Spring MVC里一般使用ModelAndView的addObject()方法将信息添加到Model里,可以在jsp页面通过request.getAttribute()方法获取(就是放到了request域里面去了),这里相当于是转发,注意转发时是把ModelAndView里的信息放到了request的attribute里面。
但是如果在ModelAndView后,使用了重定向,那么ModelAndView的信息得通过request.getParameter()方法获得(就是放到了url请求参数里),此时由于视图解析器会将Map的value放入到url作为参数,所以无论什么类型的value,都会变成String,因此,此时放入Model中的value,只能是基本数据类型和String类型,不能是自定义类型的对象数据。下面的代码运行后,重定向后浏览器地址栏为“http://localhost:8080/SecondWeb/index.jsp?param=RedictView%3F%3F%3F%3F%3F%3F%3F%3FaddObject%3F%3F%3F%3FRequest%3FParameter%3F”
package com.viewResolver;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class MyController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub ModelAndView mv = new ModelAndView("redict"); mv.addObject("param", "RedictView重定向时,使用的addObject都放到了Request的Parameter里"); return mv; }}
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean id="/myController.view" class="com.viewResolver.MyController"/> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean id="redict" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="http://localhost:8080/SecondWeb/index.jsp"></property> </bean></beans>
2.@RequestMapping里使用通配符“*”需要注意的事项:
package practice.handlers;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/home/hello")//RequestMapping放在类上面叫做命名空间public class HelloSpringController { @RequestMapping({"/first.ann","/hello.ann"}) public ModelAndView doFirst(HttpServletRequest request, HttpServletResponse response) { ModelAndView mv = new ModelAndView("/WEB-INF/jsp/welcome.jsp"); mv.addObject("message", "执行doFirst方法"); return mv; } @RequestMapping("/second.ann") public ModelAndView doSecond(HttpServletRequest request, HttpServletResponse response) { ModelAndView mv = new ModelAndView("/WEB-INF/jsp/welcome.jsp"); mv.addObject("message", "执行doSecond方法"); return mv; } //这里third前面有且只能有一级,不管这级是什么 //比如url为http://localhost:8080/项目名/home/hello/test/third.do //如果没有test这级,就无法访问到 //如果是/**/third.ann,那么这里third前面可以有任意级别,包括零级 //这种情况下,下面的url都可以访问到doThird方法 //http://localhost:8080/项目名/home/hello/third.do //http://localhost:8080/项目名/home/hello/test/third.do //http://localhost:8080/项目名/home/hello/test1/test2/third.do @RequestMapping("/*/third.ann") public ModelAndView doThird(HttpServletRequest request, HttpServletResponse response) { ModelAndView mv = new ModelAndView("/WEB-INF/jsp/welcome.jsp"); mv.addObject("message", "执行doThird方法"); return mv; }}
阅读全文
4 0
- Spring零散记
- 零散记8-7~8-9
- 零散记8-10~8-11
- 散记
- 散记
- 散记
- 散记
- 散记
- 散记
- 散记
- 零零散散记不住,maven工程读取src下配置文件
- 09月15日学习杂记(SPRING散记)
- spring AOP零配置
- Spring Struts2 零配置
- Spring讲解------------零配置
- Spring 零配置
- spring的零配置
- spring MVC 零配置
- AVAudioSession 1
- Emoji表情后台处理
- C
- 设计模式(9)--装饰模式
- Java字符串转义
- Spring零散记
- Java并发——Synchronized及其实现原理
- JAVA标示符与关键字
- 数据库设计三大范式
- Java三大框架
- vue打包后index文件一片空白
- 搜索专题: HDU1242 Rescue
- SpringCloud(一) 跟着博文学搭建
- POJ 3342