Spring 3 MVC - View Resolver for JSP pages with no controller - java

I just started the spring 3 development and I had use spring 2.5 previously.
I got stuck with the View Resolver. I had the following configuration
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
And yes it works if the jsp pages is in the /WEB-INF/jsp path. The problem that I stumble is that if I had a jsp inside (e.g. /WEB-INF/jsp/prod/Monitor/success.jsp), I cannot get it to resolve the page if I type http://localhost/Project/prod/Monitor/success.html in the browser.
Did i miss anything here. Just for more info, the jsp will show up if the jsp had a controller, but I need it to resolve jsp pages with no controller associate with it.

Add this into context:
<mvc:view-controller path="/prod/Monitor/success.html" view-name="/prod/Monitor/success" />

You can treat these pages as static resources .
see this for details.

Related

How to directly visit JSP pages with Spring loaded?

I'd like to know how to directly visit JSP pages under some directory of WEB-INF with Spring running on / path without writing any controller for view forwarding.
For example, I have a project myapp structured as follows:
src
WebRoot
`-- WEB-INF
|-- public
| `-- example.jsp
|-- views
Now, I want to visit example.jsp by directly navigating to http://localhost/myapp/public/example without implementing any controller.
What I've tried so far:
added <mvc:resources mapping="/public/**" location="/WEB-INF/public/"/> to my context xml, but it just won't work, the container keeps complaining about HTTP 404 - PAGE NOT FOUND /public/example.jsp.
added an internal resource view resolver to my context xml.
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
but I'm not sure what to do next, I maybe put many JSP pages under /WEB-INF/public, so writing a controller for each of them will be tedious.
Is there any canonical way to do this? please help!
Spring mvc we have one option
<mvc:view-controller path="/" view-name="example"/>.
This redirects the example.jsp page when you type / in the browser
Try something like that in your dispatcher servlet:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
Where the jsp folder contains your jsp files. From controller you can directly use the jsp name. You don't need to mention the jsp folder going by the above definition.

Image not getting displayed in Spring MVC

I know this question has been asked countless times.I also found many solutions but the image is not displaying.Getting error resource not found.I have done a fairly basic example.
Please help me.
i am new to spring
spring-servlet.xml
<context:annotation-config />
<context:component-scan base-package="com.app" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp">
</bean>
In display.jsp i have written the following code
<img src="<c:url value="/resources/img/a.jpg" />" />
I have created the resouces/img folder directly under WebContent.
The error get in browser console
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:7070/SpringStaticDemo/resources/img/a.jpg
Thank u for the help in advance and sorry for asking such a basic question.
Don't know what was wrong.But I restarted Eclipse and now everything is working fine.It is showing the image. Don't know what happened.

spring view resolver for HTML + redirect to apache web server

I have a web server which forwards the dynamic requests with .htm to tomcat (connected using mod_jk) and static resources are served from Apache web server. Consider the following spring controller which process login.htm
#RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST},
value = {"login.htm"})
public ModelAndView showLoginPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("login");
}
Corresponding viewresolver is as follows:
<!bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".html" />
</bean>
This is searching for a file login.html within the tomcat context. But the static login.html reside on htdocs folder of apache (under same domain).
Can anybody please tell me how can the view be resolved from web server?
It is resolved. In our configuration apache does not allow .html request to tomcat. Earlier I had a misconception that spring would resolve the view via URL and the view would be accessible via web server. Rather spring resolve the view internally from the defined path in viewResolver.
So
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value=".html" />
and html pages in WEB-INF/html would be sufficient to resolve the view.
Spring would never route the view resolving request through web server.

Image issue in Spring MVC

This is my project folder structure.
I have resources folder under webapp.
Source
_ main
-webapp
- resources
adminlogin.jpeg
I have one jsp page adminlogin.jsp which contain 1 image.But When ever I try to access this page I get page but not image.
This is my SpringDispatcher-servlet.xml
<i><context:component-scan base-package="ShoppingOnline" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:annotation-config />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</i>
This is my jsp page
<html>
<body>
<img src="/adminlogin.jpeg" alt="ADMIN LOGIN">
</body>
</html>
I have seen many example in your site very one has given one soultion.
but no has explain how its work in folder structure with example.I have made project in eclipse with the help of maven and In my folder structure the resources file is inside the web-inf folder.I haven't seen a single good example in this sites.
Please if u explain,explain with a good example how it is work if the resource is inside the web inf folder and when the resources is outside the web inf folder.
Please also let me know my mistake.
Thanks for helping.
Add your static files to webapps > resources
servlet.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
then static files can be accessible from the page.
in Jsp:
<img src="/resources/images/yourimage.jpg" />

Spring - Rewrite one URL to another

I have a Spring 2.5 application that contains a Flash banner. I don't have the source for the Flash component but it has links hardcoded to certain pages that end in .html I want to be able to redirect those .html pages to existing jsp pages. How can I have Spring resolve a few .html pages to .jsp pages?
My project looks like:
WebContent
|
-sample.jsp
-another.jsp
WEB-INF
|
-myapp-servlet.xml
-web.xml
I want localhost:8080/offers.html to redirect to localhost:8080/sample.jsp
Can I do this with Spring? I already have a SimpleUrlHandlerMapping and UrlFilenameViewController defined in the myapp-servlet.xml that has to continue serving the pages it already is.
In my web.xml, I have
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Update
Here is the URL mapper. If I add a controller, how do I return the jsp view that is in the WebContent directory as the view resolver includes the /WEB-INF/jsp directory.
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/page1.htm">page1Controller</prop>
<prop key="/page2.htm">page2Controller</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
I think you could benefit from the open source URL Rewriting library made by tuckey.org. The guys at SpringSource endorse this library, since it is set up for you automatically if you use Spring Roo to create a project, so it is of good quality. I have used it successfully in a number of projects.
See here for its homepage. And Skaffman is right, you want it to 'forward' instead of redirect, which is the default behaviour.
Configure it in web.xml like this:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
Then, in WEB-INF/urlrewrite.xml have an element like this:
<rule>
<from>offers.html</from>
<to>offers.jsp</to>
</rule>
I would use OCPsoft PrettyFaces or OCPsoft Rewrite for this:
With PrettyFaces:
create WEB-INF/pretty-config.xml
<url-mapping>
<pattern value="/offers.html" />
<view-id value="/offers.jsp" />
</url-mapping>
With Rewrite:
ConfigurationBuilder.begin()
.addRule(Join.path("/offers.html").to("/offers.jsp"));
I hope this helps.
~Lincoln
Firstly, I'm assuming that when you say "redirect", you really mean "forward". HTTP Redirects would not be appropriate here.
SO given that, here are some things to try:
Can't you just move the JSP files from WebContent into /WEB-INF/jsp/? You wouldn't have to change the ViewResolver definition, then.
You could try to have the controllers return a view name of something like ../../another.jsp, and hope that the servlet container resolves to /WEB-INF/jsp/../../another.jsp to /another.jsp.
The ViewResolver is only consulted if the controllers return the name of a view. Your controllers don't have to return the name of a view, they can return a View object directly, in this case a JstlView. This can point to whichever JSP you like. You can some controllers returning view names, and some returning View objects.
Remove the prefix property from your view resolver. This means you'd also have to change every existing controller, to prefix every view name they return with /WEB-INF/jsp/. Then you could refer to the JSPs under WebContent by name.

Categories

Resources