springmvc,通过后台跳转到页面后,页面里面请求的图片资源路径根本不对

紫枫叶520 2015-12-02 04:06:55
有2个内容一模一样的页面,就是页面路径不一样。一个是在page文件夹,一个直接放在webRoot下。


webRoot下的页面
<body>
页面目录:WebContent下<br/>
本次跳转使用方法${method }<br/>
picture:<img src="image/a.jpg" alt="picture" />
</body>
</html>

page下的页面
<body>
页面目录:WebContent/page下<br/>
本次跳转使用方法${method }<br/>
picture:<img src="../image/a.jpg" alt="picture" />
</body>


我在后台,用注解的方式,控制请求转发到的页面,有2个方法
package com.sudy.controller.annotation;

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.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
public class AnnotationController extends MultiActionController{

@RequestMapping(value="/method1",method=RequestMethod.GET)
public ModelAndView method1(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {

return new ModelAndView("/page/welcome","method","method1");
}

@RequestMapping(value="/test/method2",method=RequestMethod.GET)
public ModelAndView method2(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {

return new ModelAndView("/welcome","method","method2");
}
}


method1通过http://localhost:8080/springMvc1/method1跳转到page/welcome.jsp
图片路径根本不对,少了springMvc1


method2通过http://localhost:8080/springMvc1/test/method2跳转到welcome.jsp
图片路径也不对,多了个test


我就搞晕了,这设置请求的路径,以及设置返回路径,怎么影响了页面资源的路径,求大神点拨一下
...全文
827 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
W2G 2015-12-02
  • 打赏
  • 举报
回复
要加一个绝对路径的
_南天北落 2015-12-02
  • 打赏
  • 举报
回复
引用 3 楼 zifengye520 的回复:
[quote=引用 1 楼 my_God_sky 的回复:] 用绝对路径 ${pageContext.request.contextPath }.image/a.jpg springmvc跳转的时候会相对控制器跳转。就是你请求/test/method2的时候,springmvc解析器会去找method2的相对路径。你是肯定没有的。method1同理。 好好理理。
能说的细一点吗,,,我想知道为什么能请求到正确的页面,但是页面里面图片的src,method1少了“springMvc1”,而method2却多了“/test”呢。[/quote] 请求到正确路径是因为springmvc解析器帮你解析的路径。按照你的modelandview里面的路径解析。所以你能跳转正确但是访问资源的时候按照请求来返回上级目录就会出相对路径的错误。
_南天北落 2015-12-02
  • 打赏
  • 举报
回复
引用 3 楼 zifengye520 的回复:
[quote=引用 1 楼 my_God_sky 的回复:] 用绝对路径 ${pageContext.request.contextPath }.image/a.jpg springmvc跳转的时候会相对控制器跳转。就是你请求/test/method2的时候,springmvc解析器会去找method2的相对路径。你是肯定没有的。method1同理。 好好理理。
能说的细一点吗,,,我想知道为什么能请求到正确的页面,但是页面里面图片的src,method1少了“springMvc1”,而method2却多了“/test”呢。[/quote] 请求的时候,method1 request请求路径到页面,jsp页面根据请求request请进行 ..到上级目录就是test然后然后加上后面的东西image/a.jpg 并不是按照jsp所在的页面目录请求,而是按照request
SunnyKandy1224 2015-12-02
  • 打赏
  • 举报
回复
相对路径问题!
紫枫叶520 2015-12-02
  • 打赏
  • 举报
回复
引用 1 楼 my_God_sky 的回复:
用绝对路径 ${pageContext.request.contextPath }.image/a.jpg springmvc跳转的时候会相对控制器跳转。就是你请求/test/method2的时候,springmvc解析器会去找method2的相对路径。你是肯定没有的。method1同理。 好好理理。
能说的细一点吗,,,我想知道为什么能请求到正确的页面,但是页面里面图片的src,method1少了“springMvc1”,而method2却多了“/test”呢。
天佑笨笨 2015-12-02
  • 打赏
  • 举报
回复
jsp 的话,用<%=path %>
_南天北落 2015-12-02
  • 打赏
  • 举报
回复
用绝对路径 ${pageContext.request.contextPath }.image/a.jpg springmvc跳转的时候会相对控制器跳转。就是你请求/test/method2的时候,springmvc解析器会去找method2的相对路径。你是肯定没有的。method1同理。 好好理理。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧