Image is not displaying in Spring MVC - spring

I have tried all combinations to display image in my jsp.
But still image is not displaying.
I'm attaching Screenshot for the same.
In server Im getting this error
**[![Jun 21, 2016 11:16:40 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI \[/guestbook-webapp/images/Alarm_Tick_Icon_32.png\] in DispatcherServlet with name 'mvc-dispatcher'
Jun 21, 2016 11:22:15 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI \[/guestbook-webapp/images/Alarm_Tick_Icon_32.png\] in DispatcherServlet with name 'mvc-dispatcher'
Jun 21, 2016 11:22:15 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI \[/guestbook-webapp/src/images/Alarm_Tick_Icon_32.png\] in DispatcherServlet with name 'mvc-dispatcher'][1]][1]**

Spring MVC handles resources differently with simple configuration requirement in spring-dispatcher.xml file.
<mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
In your case, I think your Spring MVC XML file is in spring-config folder, and you need to configure the MVC resource tag exactly this way:
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
And, have your JSP file include the JSTL library on top of the page and then fetch the image as shown below:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<img src="<c:url value="/images/Alarm_Tick_Icon_32.png"/>"/>
This was tested on my local server and proven to be successful. Please let us know whether this approach works out on your end, or you need more examples from web.xml or spring-dispatcher.xml files.

Related

Version: WebLogic Server 12.2.1.4.0

I am getting below warnings when I start my weblogic server that is deployed with a rest service application that is developed with JaxRS2.0 and Jersey 2.22.2.
<Jul 5, 2022 5:47:39,957 PM EDT> <Warning> <JAXRSIntegration> <BEA-2192510> <Cannot add Jersey servlet for application class org.glassfish.jersey.server.ResourceConfig because ApplicationPath annotation is not set on it.>
<Jul 5, 2022 5:47:39,960 PM EDT> <Warning> <JAXRSIntegration> <BEA-2192510> <Cannot add Jersey servlet for application class org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig because ApplicationPath annotation is not set on it.>
<Jul 5, 2022 5:47:39,961 PM EDT> <Warning> <JAXRSIntegration> <BEA-2192510> <Cannot add Jersey servlet for application class org.glassfish.jersey.server.ResourceConfig$RuntimeConfig because ApplicationPath annotation is not set on it.>
I tried below steps:
install jax-rs 2.0 reference library as one of the applications using weblogic Admin console.
I have used below lib-ref in my weblogic.xml file that is bundled within WEB-INF
<wls:library-ref>
<library-name>jax-rs</library-name>
<specification-version>2.0</specification-version>
<exact-match>false</exact-match>
</wls:library-ref>
I have my weblogic-applicaiton.xml file with as below
enter <prefer-application-packages>
<!-- jersey-bundle-*.jar -->
<package-name>com.sun.jersey.*</package-name>
<package-name>com.sun.research.ws.wadl.*</package-name>
<package-name>com.sun.ws.rs.ext.*</package-name>
<!-- <package-name>org.glassfish.jersey.*</package-name> -->
<!-- <package-name>org.glassfish.hk2.*</package-name> -->
<package-name>org.jvnet.hk2.*</package-name>
<package-name>jersey.repackaged.org.objectweb.asm.*</package-name>
<!-- Jackson-*.jar -->
<package-name>org.codehaus.jackson.*</package-name>
<package-name>com.fasterxml.jackson.*</package-name>
<!-- jettison-*.jar -->
<package-name>org.codehaus.jettison.*</package-name>
<!-- jsr311*.jar -->
<package-name>javax.ws.rs.*</package-name>
<!-- asm.jar -->
<package-name>org.objectweb.asm.*</package-name>
</prefer-application-packages> here
Does anyone had similar issue in the past ? If yes, could you please help me how I can fix this ?

How can I access specific one file from resource mapping in spring

I have the following servlet mapping present -
<mvc:resources mapping="/js/**" location="/resources/js/" />
Observation:
If any web request starts with the /js request path, then it will be mapped to the resources directory, and the /** symbol indicates the recursive look for any resource files underneath the base resource directory. Here I can access js/test.js, js/test2.js
Requirement :
I want to access only one specific file(js/test.js). How can I access specific one file(js/test.js) not all files which present into /resources/js location.
Can I use below servlet mapping :::
<mvc:resources mapping="/js/test.js" location="/resources/js/" />
When you add this line
<mvc:resources mapping="/js/**" location="/resources/js/" />
Spring will add a filter rule which says
Whenever any URL with /js/** is fired, spring look for it recursively in /resources/js/ folder.
When you add following configuration
<mvc:resources mapping="/js/test.js" location="/resources/js/" />
Only URL with /js/test.js will be looked in /resources/js/ folder.
Please be mindful that any other request eg /js/jquery or js/abc will not be filtered.

Simple Spring Web Project Not Working

I've been trying to learn how to develop a Spring Web application and can't seem to get it working. Using the Spring Tool Suite, I created a new Spring Project using the Simple Spring Web Maven template, which basically is comprised of four files:
index.jsp:
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<c:url value="/showMessage.html" var="messageUrl" />
Click to enter
</body>
</html>
/WEB-INF/web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>spring-web-test</display-name>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
/WEB-INF/mvc-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Uncomment and your base-package here:
<context:component-scan
base-package="org.springframework.samples.web"/> -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
/WEB-INF/view/showMessage.jsp:
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
This all seemed simple and basic enough, so I built it using Maven and packaged it into a WAR file. I then deployed it to a local TomEE 7 server, went to http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT, and got to the very simple index.jsp page:
However, when clicking the link, which sends me to http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage.html, I get a 404 error from Tomcat:
I suspected it was because of the .html ending - based off what I understand of the dispatcher and view resolver, it would be looking for /WEB-INF/view/showMessage.html.jsp. However, trying to access http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage returns the same error. Looking at the Tomcat logs, I see
Nov 11, 2015 9:46:53 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage.html] in DispatcherServlet with name 'dispatcherServlet'
Nov 11, 2015 9:46:55 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage] in DispatcherServlet with name 'dispatcherServlet'
What am I doing wrong? The whole thing seems pretty simple, and I haven't even modified anything from the template Spring Tool Suite provides, so I'd have assumed it would work right out of the box. Unfortunately there doesn't seem to be a whole lot of resources online on developing Spring web applications for use in servlet containers, and of the ones I've found, nothing seemed to suggest I'm doing anything wrong.
Thanks.
Change your index.jsp to something like this
<c:url value="/showMessage" var="messageUrl" />
You don't need file extensions, because you've set them up in your mvc-config.xml.
Then you need a basic controller to handle the mapping. Something like this:
#Controller
public class WebController{
#RequestMapping(value = "/")
public String showMessage(){
return "index";
}
#RequestMapping(value = "/showMessage")
public String showMessage(){
return "showMessage";
}
}

Spring application beans being duplicated

I am developing a spring application using version 3.1.2 using tomcat 7 as the servlet manager. I have noticed that the beans are being created twice, and I'm not sure how to prevent that. I understand that the issue is somewhere in my web.xml or application context, but I've been unable to locate the source of the duplication.
From the tomcat logs on startup, I see the following (paraphrasing for space):
May 24, 2013 9:33:03 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
May 24, 2013 9:33:03 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Fri May 24 09:33:03 CDT 2013]; root of context hierarchy
May 24, 2013 9:33:03 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/kpi-reporter-servlet.xml]
May 24, 2013 9:33:03 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#df9978: defining beans [...]; root of factory hierarchy
May 24, 2013 9:33:04 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 1260 ms
May 24, 2013 9:33:04 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'kpi-reporter': initialization started
May 24, 2013 9:33:04 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'kpi-reporter-servlet': startup date [Fri May 24 09:33:04 CDT 2013]; parent: Root WebApplicationContext
May 24, 2013 9:33:04 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/kpi-reporter-servlet.xml]
May 24, 2013 9:33:04 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#2821db: defining beans [...]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory#df9978
Here is my web.xml and the application context configurations:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>kpi-reporter</display-name>
<servlet>
<servlet-name>kpi-reporter</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>kpi-reporter</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/kpi-reporter-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
kpi-reporter-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="."/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
If I remove the statement in the kpi-reporter-servlet.xml, then the beans don't get defined at all. If I move the context statement to the web.xml (along with the required imports) it still gets defined twice. I am suspecting that somehow the web.xml is being called twice during startup.
I've checked the webapps directory, and I only have one file and directory present, the .war file used to deploy the application, and the directory it was unpacked into.
Other .war applications I've developed also have this behavior, though they have similar xml files associated with them.
Does anyone know why this may be happening? I'm stumped over here.
Thanks in advance,
Max
You can comment out the context-param section, something like this:
<!--context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/kpi-reporter-servlet.xml</param-value>
</context-param-->
Or, rename kpi-reporter-servlet.xml to something else, and update context-param's param-value accordingly.
Spring's doc says:
Upon initialization of a DispatcherServlet, Spring MVC looks for a
file named [servlet-name]- servlet.xml...
So in your case, it's like kpi-reporter-servlet.xml is declared twice.
Sring initialize two application contexts here :
The ContextLoaderListener create an application context with the beans contained in the file referenced by your context-param tag :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/kpi-reporter-servlet.xml</param-value>
</context-param>
This is the "root" (or parent) application context.
Then your DispatcherServlet is looking for a file named SERVLET-NAME-servlet.xml and build another application context with the beans defined in the file. This application context will have the "root" application context as parent. Here your servlet's name is "kpi-reporter" so it loads all beans defined in "kpi-reporter-servlet.xml".
So, if you don't need to use a ContextLoaderListener then remove it. If you need it change the name of the file (for example : kpi-reporter-context.xml) and provide an empty kpi-reporter-servlet.xml which will be loaded by the DispatcherServlet.

org.springframework.web.servlet.DispatcherServlet noHandlerFound (No mapping found)

my jsps are under WEB-INF/jsp/ , and following is my web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Checkout</display-name>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
and here's mapping of page product.jsp which i am trying to access:
#Controller
#RequestMapping("/product.action")
public class ProductController {
/**
* Show the product selection form
*
* #return
*/
#RequestMapping(method=RequestMethod.GET)
public String get() {
return "products.jsp";
}
}
when trying to access the page from the following link:
http://localhost:8080/myapp/product.action
i am getting 404 in the browser, and i get the following warning in the console:
Jun 28, 2012 10:55:23 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myapp/product.action] in DispatcherServlet with name 'myservlet'
am i missing something in the configuration ?
please advise, thanks.
EDIT: i tried adding the viewResolver bean to applicationContext with no luck:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.myapp"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Follow the rule whatever Sunil has mentioned. I see one issue in your spring configuration xml that you don't have the
<mvc:annotation-driven />
You need this to register the Controller along with
<context:component-scan base-package="com.myapp"/>
When you specify the RequestMapping, URI should not have extension. Dispatcher servlet will omit it from the request URI while searching for URI mapping.
Use #RequestMapping("/product")
it should work.
Second thing when you are using view resolver just return the name of the JSP file. Do not append .jsp extension, InternalViewResolver will do it for you.
problem was that the controller wasn't detected.
i changed the base-package from com.myapp to com.myapp.controller, and it works fine now.
Use this
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
instead of class="org.springframework.web.servlet.view.InternalResourceViewResolver"
in your application context bean.
İf you use viewResolver in context xml , you should change get method return statemant to "products" and be sure the folder hierarchy is right
Change this <url-pattern>*.action</url-pattern> to <url-pattern>/*.action</url-pattern>
Check base-package name in .xml file when you learn from others tutorial
Hope it should work fine
Solution for this error:
org.springframework.web.servlet.DispatcherServlet noHandlerFound (No mapping found)
Simply follow these steps:
Right click on the project > Build Path > Configure Build path > Deployment Assembly > Add(right hand side) > Folder > Add your views folder > Apply and Close
Check the package name spring-servlet.xml , if it is correctly spelled or not. That might be the issue. I faced while starting the Spring MVC

Resources