Java 404 - 请求的资源不可用。(弹簧-mvc)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20163901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
404 -The requested resource is not available. (Spring-mvc)
提问by Umesh Awasthi
I am not very familiar with Spring MVC view resolver.I am trying to return a JSP from my controller.
My Controller method is getting executed properly but when returning view, i am getting 404 -The requested resource is not available
error.
我对 Spring MVC 视图解析器不是很熟悉。我试图从我的控制器返回一个 JSP。我的 Controller 方法正在正确执行,但是在返回视图时,404 -The requested resource is not available
出现错误。
this is entry in my servlet-context.xml
file
这是我servlet-context.xml
文件中的条目
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
this is how, i have defined resource structure
这就是我定义的资源结构
webapp
--WEB-INF
--views
--shop
--common
--cart
myjsp.jsp
This is how, i am returning JSP view from controller
这就是我从控制器返回 JSP 视图的方式
private final static String MYVIEW="shop/common/cart/myjsp";
@RequestMapping(value={"/shop/myMethod.html"}, method = RequestMethod.GET)
public String myMethod(HttpServletRequest request, Model model){
return MYVIEW;
}
this is my application home page URL
这是我的应用程序主页 URL
http://localhost:7777/my-shop/shop/
not sure where i am doing worng.
不知道我在哪里做的。
EditWe are using Tiles with spring and have seen this additional information
编辑我们正在将 Tiles 与 spring 一起使用,并且已经看到了此附加信息
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass">
<beans:value>org.springframework.web.servlet.view.tiles2.TilesView</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/tiles/tiles-admin.xml</beans:value>
<beans:value>/WEB-INF/tiles/tiles-shop.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
Not sure how i can add additional view resolver?
不确定如何添加额外的视图解析器?
error
错误
The requested resource (/my-shop/WEB-INF/views/shop/common/cart/myjsp.jsp)
is not available.
I noted down one more strange things, few JSPs under WEB-INF/views
are accessible but when I placed new JSP on same location it is not working and giving same error.
请求的资源(/my-shop/WEB-INF/views/shop/common/cart/myjsp.jsp)
不可用。
我记下了一件更奇怪的事情,下面的几个 JSPWEB-INF/views
是可访问的,但是当我将新的 JSP 放置在同一位置时,它不起作用并给出相同的错误。
回答by Suganthan Madhavan Pillai
I can help you to catch some points,where I found the difficulty
我可以帮你抓住一些要点,我发现困难的地方
- In your web.xml configure the correct path of servlet-context.xml under context-param
- In servlet-context.xml under context:component-scan base-package="/youcontrollerclass"/ - check whether you have given right path
- Dependency files in POM.xml
- 在您的 web.xml 中,在 context-param 下配置 servlet-context.xml 的正确路径
- 在 servlet-context.xml 下的 context:component-scan base-package="/youcontrollerclass"/ - 检查你是否给出了正确的路径
- POM.xml 中的依赖文件
this is part of my servlet-context.xml
这是我的 servlet-context.xml 的一部分
<context:component-scan base-package="mypath"/>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
回答by kyla
You are trying to use multiples view resolvers; however you have not specified the order. I am guessing thats why it is giving 404. Try this:
您正在尝试使用多个视图解析器;但是您没有指定顺序。我猜这就是它给出 404 的原因。试试这个:
NOTE:The ViewResolver with the highest order is the last resolver in the chain.
注意:具有最高顺序的 ViewResolver 是链中的最后一个解析器。
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value = "0" />
</beans:bean>
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass">
<beans:value>org.springframework.web.servlet.view.tiles2.TilesView</beans:value>
</beans:property>
<beans:property name="order" value = "1" />
</beans:bean>
回答by Joseph Lust
FYI, if your view is throwing an error, then you'll get a 404, even though the controller/view/JSP files are there. Try increasing your Spring logging level or attaching a remote debugger. I had the same issue last week and the 404 was at first misleading.
仅供参考,如果您的视图抛出错误,那么即使控制器/视图/JSP 文件在那里,您也会得到 404。尝试提高 Spring 日志记录级别或附加远程调试器。上周我遇到了同样的问题,404 起初是误导。
回答by Anand Rockzz
In my case, Maven->Update project resolved the issue! I know its relatively irrelevant, but it might someone someday!
就我而言,Maven->Update 项目解决了这个问题!我知道它相对无关紧要,但有一天可能会有人!