It seems that dispatcher-servlet unable to perform component scan using.
<context:component-scan base-package="abc" />
In my controller file (HelloController.java) under package abc. Code is written as follows:
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello"; //I have already made hello.jsp in web-inf/jsp/
}
}
My Application name is SpringMiddle. When try url as:
http://localhost:8080/SpringMiddle/hello.htm
I do have following url pattern in web.xml
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
It shows me error HTTP 404 not found.
EDIT: : it shows me warning
WARNING: No mapping found for HTTP request with URI [/SpringMiddle/hello.htm] in DispatcherServlet with name 'dispatcher'
You have to enable MVC in Spring. In xml config you can do it in such way:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
</beans>
and in JavaConfig:
#Configuration
#EnableWebMvc
public class WebConfig {
}
Please refer to Spring documentation
if you are referring to an mkyong tutorial, I didn't use the annotations: #Configuration
#EnableWebMvc, problem is you are using both annotations and xml declaration.
Annotation for setting the url:
#Controller
#RequestMapping("/hello")
you should remove this part:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
and you'll be able to visit the url:
http://localhost:8080/SpringMiddle/hello
Also make sure you have this in your web.xml:
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
the dispatcher-servlet.xml is where the component scanning is declared
Related
I have an existing MVC application that I'm using as a guide to integrate Spring-mvc into this API. Everything is configured very similarly, but I can't get it to return a resource.
I figured this was possible because I've done something similar in C#. I'm using Intellij, so I thought simply adding the framework support would be sufficient
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.valassis</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<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>/test/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
dispatcher-servlet
<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="com.valassis.rest.client" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Controller
package com.valassis.rest.client;
import com.valassis.rest.DalimGetPropertyValues;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class ClientController {
String message = "Welcome to Spring MVC!";
private String environment;
private DalimGetPropertyValues properties = new DalimGetPropertyValues();
{
this.environment = properties.getEnvironemnt();
}
// Renders the Homepage
#RequestMapping("/")
public String showEnviornment(Model model) {
model.addAttribute("message", returnEnvironment());
model.addAttribute("name", environment);
return "index";
}
private String returnEnvironment() {
String url;
switch (environment) {
case "dev5.0":
url = "Currently in dev 5.0: " + properties.getDev2Url();
break;
case "newDev5.0":
url = "Currently in newDev 5.0: " + properties.getNewDev2Url();
break;
default:
url = "Currently in Prod 5.0: " + properties.getPrd2Url();
}
return url;
}
}
index.jsp
<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p>Jersey resource
<p>Visit Project Jersey website
for more information on Jersey!
</body>
</html>
edit:
This is the response I get from the server:
org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/test/] in DispatcherServlet with name 'dispatcher'
You need to define the dispatcher-servlet in the web.xml. You have defined it in the dispatcher-servlet rather than the web.xml
This should be present in the web.xml
<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>/test/*</url-pattern>
</servlet-mapping>
Also add the following to indicate that the mvc application is annotation-driven in your dispatcher-servlet.xml
<mvc:annotation-driven />
I don't understand why If I use RestController annotation to declare a class as service, like this:
#RestController("/registration")
public class RegistrationService {
#RequestMapping(value="/",
produces="application/json")
public String initializeSession(Model model){
return "{\"success\":1}";
}
}
when I do a request like
http://localhost:8080/SpringRest/registration/
I get an 404 status and in the console:
No mapping found for HTTP request with URI [/SpringRest/registration/] in DispatcherServlet with name 'dispatcherServlet'
Everything works If I change #RestController("/registration")
with
#Controller
#RequestMapping("/registration")
and add #ResponseBody above method declaration.
This is my configuration:
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>SpringRest</display-name>
<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>
dispatcher
<?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">
<mvc:annotation-driven />
<!-- Uncomment and your base-package here: -->
<context:component-scan
base-package="it.reply.rest"/>
<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>
The declaration of RestController looks like:
#Target(value=TYPE)
#Retention(value=RUNTIME)
#Documented
#Controller
#ResponseBody
public #interface RestController
It includes #Controller and #ResponseBody but not #RequestMapping. So you need to add it. So the following shall work:
#RestController
#RequestMapping("/registration")
The correct usage is:
#RestController
#RequestMapping("/registration")
The value of #RestController is the component name.
From RestController (Spring Framework 4.3.9.RELEASE API):
public abstract String value
The value may indicate a suggestion for a logical component name, to
be turned into a Spring bean in case of an autodetected component.
#RestController not including url mapping so you need to #Requestmapping as well like
#RestController
#RequestMapping("/registration")
So, I have a home page that goes to index.jsp. Which is fine. On this page there's a search button and is supposed to bring you to room-list.jsp but instead it simply gets embedded in to index.jsp and I can't understand why.
I've tried returning "redirect:/room-list" but that seems to be trying to go to another controller rather than a jsp if that makes sense? I'm so stuck. Here's a screenshot of what's happening, as you can see there's 2 banners since room-list.jsp is getting added on top of index.jsp: http://imgur.com/fJ3BA67
Here's what's in my controller:
#RequestMapping("/home")
public String home() {
System.out.println("in controller");
return "index";
}
#RequestMapping(value = "/search", method = RequestMethod.POST)
public String showMessage2(#Valid SearchCmd searchCmd, Model model) {
System.out.println("in search controller");
return "room-list";
}
Web.xml
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml
<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="com.programcreek" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Because you are using:
RequestMethod.POST
Try to change it to:
RequestMethod.GET
and you will get redirect to room-list page.
This tutorial may help: http://krams915.blogspot.com/2011/01/spring-mvc-3-hibernate-annotations.html
He's using
#RequestMapping(value = "/persons/edit", method = RequestMethod.GET)
To get the page first, then he uses
#RequestMapping(value = "/persons/edit", method = RequestMethod.POST)
To do business stuff.
I'm running Spring 4 and am trying to build a VERY basic REST web service as a proof of concept. Here 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 id="poc" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>ws</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ws-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Here's the definition of my servlet in my application context:
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean name="/rememberName" class="com.corrisoft.air.ws.RememberNameController" init-method="init">
<property name="personService" ref="personService"/>
</bean>
Here are the messages I'm getting from Spring:
INFO: Mapped URL path [/rememberName] onto handler '/rememberName'
....
WARNING: No mapping found for HTTP request with URI [/springpoc/ws/rememberName] in DispatcherServlet with name 'ws'
Where springpoc is the name of the war file I'm deploying under tomcat. RememberNameController directly extends HttpServlet.
Can someone tell me what I'm doing wrong with my mapping?
I think this:
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
needs to be this:
<servlet-mapping>
<servlet-name>ws</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
I think the problem is that you are mixing what is intended to be a Spring Controller with plain Servlets.
Spring Controllers are not Servlets, they are regular POJOs that are used by Spring MVC for handling the requests (called by the Dispatcher Servlet)
If you want to see a modern way of creating a REST Service using Spring 4, check out this guide
Spring controllers should not extend HttpServlet, they should be built with Spring annotations. e.g.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public String hello() {
return "helloworld";
}
}
See the following tutorial for details http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/
I am using spring framework for writing a web service application. There are not html or jsp pages in the application. It is purely web service.
This is my Controller class
#RequestMapping("/*")
public class Opinion {
private FeedbackService fs;
public Opinion(FeedbackService fs){
this.fs=fs;
}
#RequestMapping(value="/givefeedback",method=RequestMethod.POST)
public void Login(HttpServletRequest request,HttpServletResponse response) throws IOException, ClassNotFoundException
{
ObjectInputStream in=new ObjectInputStream(request.getInputStream());
serialize.Feedback feedback=(serialize.Feedback)in.readObject();
fs.writeFeedback(feedback);
response.setStatus(200);
}
My 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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="poll_Web" class="sef.controller.Opinion">
<constructor-arg ref="feedbackService" />
</bean>
<context:component-scan base-package="sef.controller" />
</beans>
My web.xml
<servlet>
<servlet-name>opinionDispacher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath:repository-config.xml /WEB-INF/mvc-config.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>opinionDispacher</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> classpath:repository-config.xml /WEB-INF/mvc-config.xml
</param-value>
</context-param>
I am using the URL localhost:8080/feedback/givefeedback. The app is deployed as feedback.war. But the request is not at all forwarded to the controller. I am not sure why is this is happening. I am also not sure till which point the request goes in the chain.
You're missing
<mvc:annotation-driven/>
from your servlet context configuration. This element will discover #Controller beans and their #RequestMapping methods and register them as handlers.
You'll need to add the appropriate XML namespace and schema location for that namespace.
Make Opinion a #Controller and remove
#RequestMapping("/*")
it doesn't really serve any purpose.
You have also defined the Opinion bean twice in mvc-config.xml. Remove bean definition for bean id poll_Web.