1.1 通过HttpServletResponse的API直接输出(不需要配置渲染器)
案例: @Controller
public class RequestController{
@RequestMapping("/resp")
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.getWriter().println("hello HttpServletResponse")
}
1.2 使用HttpServletResponse 重定向到另一个视图(其他不变 )
@RequestMapping("/resp")
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.sendRedirect("index.jsp")
}
}
1.3 使用HttpServletRequest 转发(默认访问/下的index.jsp页面 不受渲染器的影响)
@RequestMapping("/resp")
public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
req.setAttribute("message","it's forword ")
req.getRequestDispatcher("index.jsp").forward(req,resp)
}
1.4直接返回jsp页面的名称(无渲染器)
@RequestMapping("/nice")
public String hello1(){
//转发方式1
return "home.jsp"
//转发方式2
return "forward:index.jsp"
//重定向方式
return "redirect:index.jsp"
}
1.5 使用modelandview 需要视图解析器 能指定跳转页面
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,
javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView()
//封装要显示到视图的数据
mv.addObject("msg","hello myfirst mvc")
//视图名
mv.setViewName("hello")
return mv
}
}
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!--结果视图的前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--结果视图的后缀-->
<property name="suffix" value=".jsp"/>
</bean>
1.6
//通过modelmap方式
@RequestMapping("/modelmap")
public String modelHello(String name,ModelMap map){
map.addAttribute("name",name)
System.out.println(name)
return "index.jsp"
}
1. 需求背景需求:spring MVC框架controller间跳转,需重定向。有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示。
本来以为挺简单的一件事情,并且个人认为比较常用的一种方式,一百度全都有了,这些根本不是问题,但是一百度居然出乎我的意料,一堆都不是我想要的结果。无奈啊,自己写一篇比较全都供以后大家一百度吧,哈哈哈。。。是这些写的不是很全都人们给了我写这篇博客的动力。
2. 解决办法
需求有了肯定是解决办法了,一一解决,说明下spring的跳转方式很多很多,我这里只是说一些自我认为好用的,常用的,spring分装的一些类和方法。
(1)我在后台一个controller跳转到另一个controller,为什么有这种需求呢,是这样的。我有一个列表页面,然后我会进行新增 *** 作,新增在后台完成之后我要跳转到列表页面,不需要传递参数,列表页面默认查询所有的。
方式一:使用ModelAndView
return new ModelAndView("redirect:/toList")
这样可以重定向到toList这个方法
方式二:返回String
return "redirect:/ toList "
其它方式:其它方式还有很多,这里不再做介绍了,比如说response等等。这是不带参数的重定向。
(2)第二种情况,列表页面有查询条件,跳转后我的查询条件不能丢掉,这样就需要带参数的了,带参数可以拼接url
方式一:自己手动拼接url
new ModelAndView("redirect:/toList?param1="+value1+"?m2="+value2)
这样有个弊端,就是传中文可能会有乱码问题。
方式二:用RedirectAttributes,这个是发现的一个比较好用的一个类
这里用它的addAttribute方法,这个实际上重定向过去以后你看url,是它自动给你拼了你的url。
使用方法:
attr.addAttribute("param", value)
return "redirect:/namespace/toController"
这样在toController这个方法中就可以通过获得参数的方式获得这个参数,再传递到页面。过去的url还是和方式一一样的。
(3)带参数不拼接url页面也能拿到值(重点是这个)
一般我估计重定向到都想用这种方式:
@RequestMapping("/save")
public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr)
throws Exception {
String code = service.save(form)
if(code.equals("000")){
attr.addFlashAttribute("name", form.getName())
attr.addFlashAttribute("success", "添加成功!")
return "redirect:/index"
}else{
attr.addAttribute("projectName", form.getProjectName())
attr.addAttribute("enviroment", form.getEnviroment())
attr.addFlashAttribute("msg", "添加出错!错误码为:"+rsp.getCode().getCode()+",错误为:"+rsp.getCode().getName())
return "redirect:/maintenance/toAddConfigCenter"
}
}
@RequestMapping("/index")
public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr)
throws Exception {
return "redirect:/main/list"
}
页面取值不用我说了吧,直接用el表达式就能获得到,这里的原理是放到session中,session在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢掉。
3. 总结
最底层还是两种跳转,只是spring又进行了封装而已,所以说跳转的方式其实有很多很多种,你自己也可以封一个,也可以用最原始的response来,也没有问题。好了,就到这儿。
其实也没有什么,但是知道了这个就很简单了,之前没搞懂,现在搞懂了,和大家分享。有问题的给我留言。
搞明白了url相对路径,根路径的问题,跳转的时候url到底该怎么写就容易理解和记忆了。
比如我现在地址栏是 http://localhost:8080/SpringMVC/index.jsp
那么当前路径就是 http://localhost:8080/SpringMVC ,即去掉原始的最后一个路由后,剩下的路径。
如何使用它?有这么个约定:
直接写路径 或者用 ./ 会把当前路径和它拼起来,形成真正的路径。
比如: <a href="hello/testServletAPI">test</a>
或者: <a href="./hello/testServletAPI">test</a>
最后拼出来都是 http://localhost:8080/SpringMVC/hello/testServletAPI
而你上面写那两个超链接里的路径,叫做 相对路径 。 ./ 是当前目录, ../ 就是再向上一级。
在做不同请求时,根路径也不一样。
如何使用根路径?
/hello 即表示 根路径+hello ,比如
<a href="/hello/testServletAPI">test</a>,访问的是 http://localhost:8080/hello/testServletAPI
直接返回string的;
返回ModelAndView的;
如果配置了视图解析器:
那么会对success加上前缀和后缀。并且跳转方式是转发。
首先明确两个事啊,转发是发生在服务端的,因此你转发可以转发到服务器的内部目录下,就是WEB-INF下,去访问它目录下的页面。但是重定向只能访问webapp下除WEB-INF的路径。
先把request和response拿进来。
服务器内部转发的根路径是 主机名/项目名 ,重定向的根路径是 主机名 。因此两者的url不太一样。
同样访问webapp下的 newPage.jsp ,重定向需要自己手动把项目名加上。
同样访问webapp下的newPage.jsp:
重定向一般用相对路径,因为省去自己写项目名。
此处 ../ 是因为当前路径是 主机/项目名/hello ,所以要再向上一级目录。形成 主机/项目名/newPage.jsp
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)