Spring controllers not found - spring

I am unable to call spring MVC controller. Everytime I hit it, it says not found.
My files are as following.
Web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/gk1</url-pattern>
</servlet-mapping>
spring-servlet.xml
<mvc:annotation-driven />
<context:component-scan base-package="com.gkool" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans>
My controller file
package com.gkool;
#Controller
#RequestMapping("/score")
public class ScoreController {
Logger log = Logger.getLogger(ScoreController.class);
#RequestMapping(value = "", method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
#RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
public ModelAndView hello(#PathVariable("name") String name) {
ModelAndView model = new ModelAndView();
model.setViewName("hello");
model.addObject("msg", name);
return model;
}
}
when I start sever and hit URL http://localhost:8080/gk1/score or http://localhost:8080/gk1/score/hello/anoop It gives error code 404 not found.
P.S. /gk1 is module name in tomcat.
When I start the tomcat server it gives following logs
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/score/hello/{name}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.gkool.ScoreController.hello(java.lang.String)
Feb 07, 2017 10:37:11 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping registerHandlerMethod
INFO: Mapped "{[/score],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.gkool.ScoreController.printWelcome(org.springframework.ui.ModelMap)
Feb 07, 2017 10:37:12 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
INFO: Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
That means it identifies a mapped URL as /score/hello/{name} but when I start my server in debug and put a debugger breakpoint in the controller method, the debug control doesn't come there.
can anyone please help?

Check if you've defined the context web within the tomcat configuration of your application:
<Context docBase="/path/to/myapp.war" path="/myContext" reloadable="true"/>
Now execute the request like this:
http://localhost:8080/myContext/gk1/score/hello/anoop
But if you've defined like:
<Context docBase="/path/to/myapp.war" path="/gk1" reloadable="true"/>
Then in the web.xml servlet configuration you just have to define the servlet mapping like this:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Fix your Spring configuration.
<mvc:annotation-driven />
is a configuration element.
This is required for Spring to "find" your controllers:
<context:component-scan base-package="your.controller.package"/>
Per the Sotirios Delimanolis comment, the following configuration
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/gk1</url-pattern>
</servlet-mapping>
Tells tomcat to send requests with exactly match this URL: http://localhost:8080/gk1 to the spring servlet.
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/gk1/*</url-pattern>
</servlet-mapping>
Which tells tomcat to send any request that starts with the URL http://localhost:8080/gk1 to spring.
The * is the difference between: "exactly match" and "starts with".

Related

No mapping found for HTTP request with URI in two DispatcherServlet

My web.xml states dispatcher-servlet url pattern as:
<!-- root context -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
</servlet>
<!-- admin context -->
<servlet>
<servlet-name>adminServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/admin-context.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>adminServlet</servlet-name>
<url-pattern>/admin/</url-pattern>
</servlet-mapping>
two servlet-mapping files are described as
<!--servlet-context.xml for appServlet-->
<annotation-driven />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!--admin-context.xml for adminServlet-->
<annotation-driven />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/admin/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
and I coded two controllers for the test. one is ok but the other met "error"
#Controller("admin.IndexController")
#RequestMapping("/admin")
public class AdminIndexController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
...
return "index";
//I thought this location will be /myContext/WEB-INF/view/admin/index.jsp
//and it run well
}
}
but the following controller couldn't find it's next page.
#Controller
#RequestMapping("/admin")
public class AddCategoryController {
#RequestMapping(value={"/category/add"}, method=RequestMethod.GET)
public String form(Model model){
...
return "addCategory";
//I thought the location will be /myContext/WEB-INF(/view/admin/category/)index.jsp
//but Spring forwarded to /myContext/WEB-INF/view/addCategory.jsp
}
}
if I write the code as "return admin/index;" this has no problem.
But I think AddCategoryController has to be handled by adminServlet and admin-context.xml.
Because AddCategoryController calls addCategory.jsp as the way of relative path, so they both should be on the same path. but I met fail... Can you tell me what is wrong?
You can achieve everything by registering servlet only once. Do not know why you are registering DispatcherServlet twice. You can give the root jsp folder path like "/WEB-INF/view/ and then in code you can move to different jsps in subfolders like /admin/index.jsp

How do I map this URL to a Spring controller method?

I'm using Spring 3.1.1.RELEASE. I'm having fits mapping URLs to controller methods. I would like to map the URL "/my-context-path/organizations/add" to the controller method below. In my controller, I have
#Controller
#RequestMapping("/organizations")
public class OrganizationController
{
…
#RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView doGetadd()
{
… do some stuff …
return new ModelAndView("organizations/add");
} // doGetadd
In my web.xml I have
<?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_2_5.xsd"
version="2.5">
<display-name>SB Admin</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/organizations/*</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>
and in my dispathcer-servlet.xml I have
...
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan base-package="org.myco.subco" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
but requests for my desired context-path result in "No mapping found for HTTP request with URI [/myproject-1.0-SNAPSHOT/organizations/add] in DispatcherServlet with name 'dispatcher" errors (using JBoss 7). How do I map this thing properly? Note that I have multiple methods in my controller that I want to different URLs within the "/organizations" space.
Try this.
Change the dispatcher servlet mapping to :
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And for the OrganizationController the mapping would be
#Controller
#RequestMapping("/organizations")
public class OrganizationController
And for the ContractsController the mapping would be
#Controller
#RequestMapping("/contracts")
public class OrganizationController
According to the Spring Doc the ModelAndView constructor parameter is the name of the view file.
So that file could be addView.jsp .
As well as the fact that you're (as far as my Spring knowledge goes) actually mapping it to /Application-Name/organizations/organizations/add due to :
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/organizations/</url-pattern>
</servlet-mapping>
And
#Controller
#RequestMapping("/organizations")
public class OrganizationController
I'd recommend changing the requestmapping from the controller to
#Controller
#RequestMapping("/")
public class OrganizationController
The <url-pattern>/organizations/</url-pattern> basiccally defines the 'virtual path' on which your site will be accessible.
Al mappings you do on controllers will append to it, makeing it /organizations/whateverpagecomeshere.jsp
And make sure that View file exists !

The spring web mvc framework handle the jsp file dispatch as another request

I am new to spring web mvc framework,and I use struts 2 before.
I create a new dynamic web project using eclipse EE,and add all the jars to the /web-info/lib.
The whole hierarchy of the project is like this:
SpringMVCTest
WEB-INF
web.xml
example-servlet.xml
jsp
hello.jsp
lib
xxxx.jars
.....
This is the servlet definition:
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This is the example-servlet.xml:
<context:component-scan base-package="com.kk.web.controllers" />
<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>
And the controller:
package com.kk.web.controllers;
#Controller("example")
#RequestMapping("/example")
public class ExampleController {
#RequestMapping("/hello")
#ResponseBody
public String hello() {
return "hello";
}
#RequestMapping("/hello_jsp")
public ModelAndView hello_jsp(){
ModelAndView mv=new ModelAndView("hello");
mv.addObject("message", "welcome");
return mv;
}
}
It worked when I run:
http://localhost:8080/SpringMVCTest/example/hello
But when I run:
http://localhost:8080/SpringMVCTest/example/hello_jsp
I got the warn:
2011-10-17 10:36:15 org.springframework.web.servlet.DispatcherServlet noHandlerFound
Warn: No mapping found for HTTP request with URI [/SpringMVCTest/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'example'
It seems that the ExampleController works,it dispatch the request "/example/hello_jsp" to the right view "jsp/hello.jsp".
But then the spring take the file dispatch "/jsp/hello.jsp" as another request,then it will not find the matched url mapping in the "example" controller.
Why?? IMO,a requst must come from the client to server,the controller receive only one request here "/exmaple/hello_jsp",isn't it?
And How to fix it?
BTW,I can set the url pattern to "/*.xxx",but I do not want the suffix in the url.
Any ideas?
Previous answer did not work...
This posting looks similar: http://forum.springsource.org/showthread.php?55982-No-mapping-found-for-HTTP-request-with-MVC-requests
Summary: change
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
to
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and checking my latest Spring MVC app I use the latter pattern (no * on the end).

mvc:view-controller causes PageNotFound in Spring Tiles2

I have a webapp based on Spring 3.0.6 which works fine on Tomcat 7.0.
The web.xml defines the dispatcher as following:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The dispatcher defines the view resolver in the usual way:
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-def.xml</value>
</list>
</property>
</bean>
I have a controller annotated with #RequestMapping("/home") and a "home" view defined in tiles-def.xml. When I point my browser to the /myapp/home.html, the Tiles page is opened.
If I add <mvc:resources mapping="/resources/**" location="/resources/" /> or <mvc:view-controller path="/" view-name="home.html"/> to my dispatcher xml file, pointing the browser to /myapp/home.html results in a 404. The log says:
21:34:22,128 WARN PageNotFound:947 – No mapping found for HTTP request with URI [/myapp/home.html] in DispatcherServlet with name 'dispatcher'
What am I doing wrong?
Thanks a lot
The problem in my application was due to the automatic view name resolution. My annotated method in my #Controller returned void, and the framework tried to guess the tiles view name using the request path.
I modified my annotated method as following, returning a String:
#RequestMapping(value="/page", method = RequestMethod.GET)
public String showForm(HttpServletRequest request, Model model) {
// TO BUSINESS LOGIC
// return tiles view name as configured in 'tiles-def.xml'
return "my_tiles_view_name";
}
With this change, everything works fine.

Spring MVC: "No mapping for [...] in DispatcherServlet with name 'dispatcher'"

Can someone help me. In Spring MVC I'm getting the error
WARNING: No mapping for [/TechBooks/details.htm] in
DispatcherServlet with name 'dispatcher'
1) First of all, in web.xml, I use the standard DispatcherServlet which intercepts all *.htm, nothing unusual here, this was pre-written for me:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
2) In dispatcher-servlet.xml, I am using the SimpleUrlHandlerMapping, again this is standard and pre-written:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
3) Also in dispatcher-servlet.xml, I define my FormController class called "DetailsFormController", that maps to details.htm:
<bean name="/details.htm" class="techbooks.web.DetailsFormController"/>
4) And the class DetailsFormController is a FormController for a form.
package techbooks.web;
public class DetailsFormController extends SimpleFormController {
....
}
When I execute the resource /details.htm, however, I get the above error.
WARNING: No mapping for [/TechBooks/details.htm] in
DispatcherServlet with name 'dispatcher'
Any ideas would be appreciated. Thanks.
Can you change your bean difinition to
<bean name="/TechBooks/details. htm" class="techbooks.web.DetailsFormController"/>
Or write a urlmapping handler for TechBooks
I found out what the issue was.
When using the SimpleUrlHandlerMapping, the Controller name has to be DetailsController, not DetailsFormController, so it maps automatically to the right JSP.

Resources