Integrating Spring MVC with existing application - spring

I am trying to integrate Spring MVC with my existing application. Existing application has context root as PORTAL here is my Spring MVC setting in web.xml is
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Everything works fine and I am able to access pages through Spring MVC. Pages get data from Controller but pages doesn't load the css, img and js files.
Application Folder structure is
src
webapps
-css
-img
-js
-WEB-INF
--jsp
JSP
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="css/poc.css"/>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.js"></script>
Error
No mapping found for HTTP request
with URI [/PORTAL/css/bootstrap.css] in DispatcherServlet with name 'spring'

As Spring MVC works on URL Pattern and every request for Image and Resources came from client is separate request, So Spring is finding the matching pattern for "/bootstrap.css" to identify is there anything to serve against this Pattern.
And there is no resource mapped against this that is why you are getting this error.
You can say Spring that these are my resources and don't go for any mapping finding, if request comes with mentioned URL.
put below line in spring-servlet.xml file.
<mvc:resources mapping="/PORTAL/css/" location="/PORTAL/css/" cache-period="0" />
this will help you to get rid of css loading errors. but problem for js and image still be there,
So try to keep all resources in one resource folder and give mapping like one below,
<mvc:resources mapping="/resources/**" location="/resources/" cache-period="0" />
Also, import this xsd above in xml file,
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
hope this helps.

You are missing the ContextPath while refering the js and css files.
Prefix the resource references as mention below
<script src="${pageContext.request.contextPath}js/jquery.js"></script>

For me the problem is that you map the SpringDispatcherServlet to everything ( <url-pattern>/</url-pattern> ). You could map it to only one type of resource and then let the server serves the other types. Example :
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Then all you css, images, videos, applet jars etc will be served directly by the server while urls finishing with .htm will be served by Spring MVC.
If you need, you can also define multiple servlet mapping for the Dispatcher servlet.

Related

welcome-file does not work in WebLogic

When I deploy my web application (Spring MVC) on WebLogic, the welcome page does not launch with the default URL http://my.site.com/myApp. I have my welcome page under home directory and I set as follows in web.xml.
<welcome-file-list>
<welcome-file>/home/index.html</welcome-file>
</welcome-file-list>
I am able to access the page using the full URL http://my.site.com/myApp/home/index.html.
Also, if I put index.html directly under the root and update web.xml as follows, the welcome page launches with the default URL
<welcome-file>/index.html</welcome-file>
What should I do to make the default URL launch the index.html under home directory ?
Here is the code in web.xml and applicationContext.xml.
web.xml:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
applicationContext.xml:
<mvc:default-servlet-handler/>
I assume it has something to do with the leading slash. Have you tried simply home/index.html instead of /home/index.html.
This user asked the same question but had it work without the initial slash: can-i-set-tomcat-with-a-welcome-file-in-a-subfolder
Another discussion on the same topic:
I also had the same issue.
Make sure that the file you are trying to show is outside the
web-inf folder.
There are some configurations available in weblogic.xml file but none seems to work.I think it's a security feature or something else not sure exactly what.
In my case dispatcher servlet was capturing the empty request and showing no mapping found. May be you can create a controller mapping for the same and be done with it.
The funny thing is the same thing was working flawlessly in tomcat 7

java.lang.IllegalStateException: Cannot forward after response has been committed while including HTML files using jsp:include

When <jsp:include> is used for including HTML file DispatcherServlet is throwing
java.lang.IllegalStateException: Cannot forward after response has been committed
I have one servlet:
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In it, I have enabled Spring MVC annotations and have handler mapping and adapter for JSP files without controllers (converting old webapp to Spring). And I have enabled DefaultServletHttpRequestHandler in this Servlet.
Any idea how to avoid that IllegalStateException when including html files?
So if you let spring handle all html files, it will always fail on jsp:include because spring cannot handle html includes.
Best way around this for me was to leave html files on default servlet.
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
and leave rest on DispatcherServlet.
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
This is definitely not a best solution, but until I convert all jsps (around 1000 of them) to mvc and something like tiles this is the only way I can see it working.
It is illegal to call forward() after some response is written to the output stream. The response may have been already sent to the client.
This article Causes of Response already committed explains why response is already committed.
I tried the proposed solution which declares the default servlet mapping for *.html url patterns and it worked fine. The only problem was that it introduced some side effects in my case (an hybrid webapp, spring and non-spring managed): html files that should have been managed by Spring's front controller now were managed by Tomcat's default controller.Fortunately, I found a couple of solutions with zero impact on the rest of the webapp.
Use .jsp file extension instead of .html. Spring won't complain if
it finds <jsp:include page="file.jsp" /> instead of <jsp:include page="file.html" />
Include the .html file in a scriptlet <%=file.html %> and avoid using the jsp "include" tag

How to get a trivial case of Spring MVC view (JSP) resolving to work?

My app uses Spring MVC (latest; 3.2.2) to create a RESTful API returning JSON, and so far I haven't needed a view layer at all. But now, besides the API, I need a simple utility page (plain dynamic HTML) and wanted to use JSP for that.
I want requests to http://localhost:8080/foo/<id> to go through a controller (Java) and end up in a JSP. Should be simple, right? But I'm getting 404; something is not right in resolving the view.
HTTP ERROR 404
Problem accessing /jsp/foo.jsp. Reason:
Not Found
Controller:
#RequestMapping(value = "/foo/{id}")
public String testing(#PathVariable String id, ModelMap model) {
model.addAttribute("id", id);
return "foo";
}
Defining controllers and mapping requests works; this method gets called just fine.
Spring config:
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/jsp/" p:suffix=".jsp"/>
The problem is probably here. I've experimented with slightly different prefixes and putting the JSPs under WEB-INF, as well as stuff like <mvc:view-controller path="/*" /> but no luck yet.
(Do I even need to specify InternalResourceViewResolver, or should default view resolvers take care of this?)
JSP files. Under src/main/webapp/jsp (the project uses Maven conventions) I obviously have the JSPs.
Is there something wrong with this location?
web.xml:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I have browsed through Spring MVC documentation, but my problem is probably too trivial and obvious to easily find help there. :-P
Can anyone enlighten me on what I'm doing wrong?
I think what you need to do is changing
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
/* won't match if there is another folder in the path, like /jsp/foo.jsp. On the other hand / will match everything.

servlet mapping is shown as the project name on the URL. do i need ibm-web-bnd.xmi?

I have created a servlet mapping as /reskilling in my Servlet class. When I run the application the url contains the project name instead. This is a WebSphere Web application which is part of my EAR project. Do i need ibm-web-bnd.xmi to fix this?
here's my web.xml
<display-name>HibernateReskillingWeb</display-name>
<servlet>
<description>Paid Up Plan List</description>
<display-name>PaidUpPlanServlet</display-name>
<servlet-name>PaidUpPlanServlet</servlet-name>
<servlet-class>za.co.test.PaidUpPlanServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PaidUpPlanServlet</servlet-name>
<url-pattern>/reskilling</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
The erros information is shown below ...
HTTP Error Code: 404
Error Message:JSPG0036E: Failed to find resource /HibernateReskillingWeb/views/PaidUpPlan.jsp
Root Cause:java.io.FileNotFoundException: JSPG0036E: Failed to find resource /HibernateReskillingWeb/views/PaidUpPlan.jsp at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionProcessor.findWrapper(AbstractJSPExtensionProcessor.java:395)...
What is the context root for this web application? From the error message it is HibernateReskillingWeb
If you haven't done anything to the Context Root, the default value is the Dynamic Web project's name.
You can see this value in the application.xml and you can change that to anything that you want.
Is there a directory called views under which your JSPs are stored? That is where the Container is looking for your JSP.
The servlet mapping (that you have shown) has got no role to play here as you are trying to access a JSP.
HTH

Static resources in Spring MVC app

In my Spring mvc application I want to serve static resources using mvc:resources.
My web.xml mapping looks that:
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Where main is dispatcher servlet to serve all the content
In my servlet.xml file I added:
<mvc:resources mapping="/static/**" location="/static/"/>
and it works properly when my application context is empty (like localhost:8080/), but when I deploy application in another context it doesn't work, I got 404.
I tried many combinations:
"static/**"
"*/static/**"
Nothing works.
I'm sure it's server context problem, but I have not idea (I couldn't find the solution in Google too) how to solve this. Please, help.
I was able to succesfully map my static resources using the following convention:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
The easiest way for we that worked, was adding in the servlet-config.xml (the file that is configured in web.xml as the contextConfigLocation) the following:
<mvc:default-servlet-handler/>

Resources