问题
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.
this is entry in my servlet-context.xml
file
<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
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
http://localhost:7777/my-shop/shop/
not sure where i am doing worng.
Edit We are using Tiles with spring and have seen this additional information
<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.
回答1:
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.
回答2:
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
this is part of my 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" />
回答3:
In my case, Maven->Update project resolved the issue! I know its relatively irrelevant, but it might someone someday!
回答4:
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:
NOTE:The ViewResolver with the highest order is the last resolver in the chain.
<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>
来源:https://stackoverflow.com/questions/20163901/404-the-requested-resource-is-not-available-spring-mvc