I am getting the following error:
No mapping found for HTTP request with
URI [/my-app] in DispatcherServlet
with name 'web'
My web.xml looks like:
<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>
And my web-servlet.xml looks like:
<bean name="myController" class="com.app.web.MyController" />
<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>
Any help / explanation would be great. Also, what should the view parameter be to new ModelAndView(?) in the controller?
My goal is to be able to hit http://localhost:8080/my-app and be routed to MyController which would then load a given jsp.
Your configuration looks fine to me. In your MyController, make sure you have a request mapping for my-app, like this:-
#Controller
public class MyController {
#RequestMapping(value="/my-app", method=RequestMethod.GET)
public String mainPage() {
return "index";
}
}
When you call http://localhost:8080/my-app, the server will return the index.jsp from /WEB-INF/jsp/ folder.
Looks like DispatcherServlet is trying to process the request for /my-app, which suggests to me that your web.xml servlet-mapping is directing requests for that space to DispatcherServlet.
You might have something like this?
<servlet-mapping> <servlet>dispatcher</servlet> <url-pattern>/*</url-pattern> </servlet-mapping>
Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit
<servlet-mapping> <servlet>dispatcher</servlet> <url-pattern>*.do</url-pattern> </servlet-mapping>
or change /* to /
Hope that helps.
Related
When I am trying to execute #RequestMapping("/showForm") I am facing an error.
I think my code seems fine, I am simply returning new String with the name of my JSP file - "mainmenu.jsp". I have this folder the folder JSP in the right place.
The error:
Description The origin server did not find a current representation
for the target resource or is not willing to disclose that one
exists.
Where can be the problem?
<mvc:annotation-driven />
<context:component-scan
base-package="com.crunchify.controller" />
<mvc:default-servlet-handler />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<servlet>
<servlet-name>crunchify</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/mainmenu.jsp</url-pattern>
<url-pattern>/mainmenu.html</url-pattern>
<url-pattern>/processForm.jsp</url-pattern>
<url-pattern>/processForm.html</url-pattern>
<url-pattern>/index.jsp</url-pattern>
<url-pattern>/main-menu.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
The actual request:
#RequestMapping("/showForm")
public String helloWorld() {
return "mainmenu";
}
Your deployment descriptor(web.xml) does not have mapping for the url you are trying to access. Either add "/showForm" in url mapping for dispatcher servlet or use a wild card "/" in your url mapping for your dispatcher servlet. like,
<servlet>
<servlet-name>crunchify</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
As web.xml is the entry point, there should be url mapping.
(And also you can map different url for different dispatcher servlet. In other dispatcher servlet you can use another view resolver.)
Hope <url-pattern>/</url-pattern> it will work for you.
Go to your apache web server config, its probably in ProgramFiles > Apache Tomcat folder
Go to tomcat>conf folder
Edit server.xml
Search "Connector port"
Look for the connection timeout, if its set to -1 then that is the issue
Change it to "20000", save the file. Restart the Apache Service
Problem will be fixed.
I searched all over the site and nothing suggested worked!
I'm tryng to make a spring-mvc and angularJS application; This is my project structure:
angularState
-src/main/java/it.controller.DefaultController.java
-src/main/webapp
--index.jsp
--WEB-INF
---dispatcher-servlet.xml
---web.xml
---static
----default.html
so in my web.xml I have:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
in the dispatcher-servlet.xml:
<context:component-scan base-package="it.controller"></context:component-scan>
<mvc:annotation-driven />
<context:annotation-config />
<mvc:resources location="/WEB-INF/static/" mapping="/static/**" />
in the index.jsp:
<% response.sendRedirect("default"); %>
then in the controller:
#Controller
#RequestMapping("/default")
public class DefaultController {
public String showDefault(){
return "/static/default.html";
}
}
I've always used jsp and everything was fine; now I've tried every single question of the site:
I've tried
-mvc:default-servlet-handler
-InternalResourceViewResolver with prefix "/WEB-INF/static" and suffix ".html" or even suffix void
-InternalResourceViewResolver void and the controller method returning "/WEB-INF/static/default.html"
NOTHING!
nothing showed the default.html. why that .html file is so complicated!
This is because normally *.jsp style uri patterns are handled by the servlet container and in your case *.html is not being handled by the container and instead the path is being delegated to Spring MVC which does not know how to render these extensions
As a quick fix you can try below code in conf/web.xml
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Or
Content negotiating view resolver can also be used in place of InternalResourceViewResolver(I Guess you might be using this) which return a suitable view based on file extension declared in “mediaTypes” property)
Example
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="html" value="text/html"/>
</map>
</property>
For anyone who will google, here the answer.
the problem was pretty simple: All I was wrong was the controller method; the original controller method was:
#Controller
#RequestMapping("/default")
public class DefaultController {
public String showDefault(){
return "/static/default.html";
}
}
the correct version must be:
#Controller
#RequestMapping("/default")
public class DefaultController {
#RequestMapping(method=RequestMethod.GET)
public String showDefault(){
return "/static/default.html";
}
}
because every page must have a GET access, more than all, the homepage!
Because of that missing #RequestMapping on the method, every config I tried gave me errors on the .html files. As I sayd, .html files only needs the
<mvc:resources location="/WEB-INF/static/" mapping="/static/**" />
to be configured because they're static resources and they don't need the help of the servlet. Hope this will help someone, thanks to everyone who tried.
Recently I've got a problem with simple Spring MVC Application, particularly with controller.
Here is my controller code snippet:
#Controller
public class MyController {
#RequestMapping(value="/", method=RequestMethod.GET)
public String m1(Model model){
return "form";
}
}
Here is my web.xml
<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/config/config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And here is bean configuration file:
<context:component-scan base-package="ua.macko.controller"/>
<mvc:annotation-driven/>
<mvc:resources location="/resources/" mapping="/resources/**"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
In the jsps folder I've got two jsp files: hello.jsp and form.jsp.
When I'm trying to achieve root page of application, such as http://localhost:8080/WebTest/ I'm ALWAYS getting hello.jsp REGARDLESS of what value I return in m1 method...whether it is "hello", "nothello", "iamnotapage" etc.
I am using Eclipse and Spring 3.2.8.
When you start Spring application in log you should see something like this :
INFO: Mapped URL path [/] onto handler 'welcomeController'
If you do not see your controller on this list it means you should check:
context:component-scan base-package="ua.macko.controller"
because your controller is not visible to Spring
If you see two mappings than yours is overridden.
If you do not see any mapping like this than most probably Beau Grantham is right, or some other config problem is there.
Is the path correct? I see you have jsps, try /WEB-INF/jsp/?
<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>
I'm newbie to spring mvc and trying to develop a very basic login webapp. I get the below error while running the project. I have tried almost everything and could not fix this error since last two weeks. Please can someone help me.
May 21, 2013 2:37:12 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringWeb/WEB-INF/jsp/loginnn.jsp] in DispatcherServlet with name 'spring'
My jsp pages resides under WEB-INF/jsp. The method loginpage in my controller is gettting invoked but the view name is not being rendered and resolved. Greatly appreciate your help.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>testspring</display-name>
<servlet>
<servlet-name>frontcontrol</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>frontcontrol</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
frontcontrol-servlet.xml
<context:component-scan base-package ="com.shell.spring.testspringapp">
</context:component-scan>
<bean id ="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Controller class
#Controller
public class Firstcontrol {
#RequestMapping(value="/")
public ModelAndView invokeme(Model m)
{
ModelAndView mav=new ModelAndView();
mav.setViewName("result");
System.out.println("In method");
return mav;
}
#RequestMapping(value="/submit" ,method=RequestMethod.GET)
public String submit(Model m)
{
System.out.println("In submitmethod");
return "submit";
}
}
Since you're mapping your DispatcherServlet to '/' try adding <mvc:default-servlet-handler/> to your spring-servlet.xml
I saw same error, turned out that you need enable MVC annotations separately
<mvc:annotation-driven/>
In addition to
<context:Annotation-config/>
<context:component-scan base-package="your base packege " />
You should check the following section in spring configuration xml. Maybe you copied it from somewhere and forgot to make it your package name. Spring will not be able to scan packages if it so and eventually show this error.
In the web.xml file the configuration file (spring-servler.xml) should be specified to be used by the dispatcher servlet, since your not using the conventional name, which is the [servlet-name]-context.xml.
Since Spring cannot find the configuration file for the dispatcher servlet the viewresolver is never registered. I assume that 'spring-servler.xml is located within the WEB-INF folder in my example, so you may need to adjust.
Also notice I switched the servlet mapping to / which helps if you need to resolve static resources, since / acts as a catch all as opposed to mapping everything through the dispatcher.
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servler.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
You could also try using a regular view resolver, if your not using JSTL.
Replace the jstl view resolver:
<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>
With:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Try returning a String from your controller that specifies the view name:
#RequestMapping(value="/")
public String loginpage(Model model)
{
Employee emp=new Employee();
ModelAndView mav=new ModelAndView();
model.addObject("emp", emp);
return "loginnn";
}
#sanjay and #Will Keeling are both right. It is required to use <mvc:default-servlet-handler/> (as the last handler) and enable both <mvc:annotation-driven/> and <context:Annotation-config/>
I have the following in my web.xml file:
<servlet>
<servlet-name>onBoardingUI</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
and in my sample-servlet.xml file:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<!-- <property name="maxUploadSize" value="100000" /> -->
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/fileupload.form=fileUploadController
</value>
</property>
</bean>
<bean id="fileUploadController" class="com.wrightexpress.si.onboardingui.web.FileUploadController">
<property name="commandClass" value="com.wrightexpress.si.onboardingui.service.UploadFile" />
<property name="formView" value="process-file" />
<property name="successView" value="results" />
</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>
Now when I deploy the application, I get a 404 when hitting the context root. No exceptions or anything in the server log. I realized that I am setting the URL handler, but for some reason no requests are getting through. I've tried various forms of declaring the servlet-mappings in web.xml to no avail. I have a simple file upload form that has an action of fileupload.form.
Thanks!
EDIT: I have a series of jsp pages that are currently being served up via the viewResolver defined above. These stop working when I add the urlMapping bean in there. Now, I don't know how I should handle this, if I just apply a servlet-mapping of /* in the web.xml file, how do I specify in the sample-servlet.xml file which controller to tie each jsp to other than individually? Or how do I keep my web.xml like it is and only have the defined URL handler handle the fileupload.form action?
are you sure your web.xml ist right?
you have a DispatcherServlet called "onBoardingUI" but your servlet-mapping tags do look for a servlet called "sample".
shouldnt the servlet-mapping be:
<servlet>
<servlet-name>onBoardingUI</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>onBoardingUI</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>onBoardingUI</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
Once you start defining URL Mappings, you will need to tell spring mvc how to handle any URLs not specifically mapped. Try adding the following mapping:
/*=urlFilenameViewController
and the following bean to handle these requests:
<bean id="urlFilenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
The UrlFilenameViewController will pass the URI directly to the view resolver. e.g. example.com/index.html will get mapped to WEB-INF/jsp/index.jsp
If you need to use the full path of the URI, (e.g. example.com/help/index.html maps to WEB-INF/jsp/help/index.jsp)
then set the alwaysUseFullPath property on the URL Mapping
<property name="alwaysUseFullPath" value="true" />