servlet mapping always 404 error - java

This is this web.xml file
<!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>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-ap>
applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.tutorial.ejemplospring" />
<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>
and this is the controller
package com.tutorial.ejemplospring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class MainController {
#RequestMapping("/main.html")
public ModelAndView mainPage() {
return new ModelAndView("main");
}
}
I don't know why ´http:localhost:8080/ejemplospring/mail.html´ doesn't match with the url pattern .html?, I get a 404 error and if I try with / or / I get the same error.

Try to uncomment
<context:component-scan base-package="com.tutorial.ejemplospring" />
Seems that it is not finding the Controller and therefore not registering the Mapping.

Why do you have your ComponentScan commented out? You need to enable this (uncomment), so it finds your #Controller
This
<!-- <context:component-scan base-package="com.tutorial.ejemplospring" />-->
needs to be:
<context:component-scan base-package="com.tutorial.ejemplospring" />

First issue is that you have to enable component scanning for the beans to be created.
Second one is that Spring MVC by default ignores the the extension in the URL, as it's to be used for specific purposes like .json/.xml would indicate to Spring MVC that you need data in JSON or XML format.
So your URL should be mail.html.someExtension ie. mail.html.do.
If you don't want to do that and you absolutely must have the extension as it is then you can use following configuration to enable extension mapping.
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
</bean>
Otherwise simplest way would be to just use some simple URL like /mail without any extension.

Related

Application starts index.jsp not mail-menu.jsp

I have a problem with my demo app.
Why is the application launch index.jsp not main-menu.jsp as I mapped in HomeController? And as I see via debugger HomeController is not used.
Where is the problem and how can I fix it?
Thanks
Project Structure Screenshot
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc-demo-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.aalegz.springdemo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HomeController.java
package com.aalegz.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#RequestMapping("/")
public String showPage() {
return "main-menu";
}
}
I am not so sure what is the problem but all I could do is suggest you some alternative. Try configuring your controller as:
public class HomeController extends AbstractController{
#Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("main-menu");
model.addObject("msg", "Lets Check");
return model;
}
}
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>spring-mvc-demo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And, your spring-mc-demo servlet.xml as:
<bean name="/welcome.htm"
class="com.aalegz.springdemo.mvc,HomeController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

Spring MVC controllers not loaded

I have a problem with Spring not picking up the controller. I've used maven webapp archetype to create a project in Eclipse and it worked fine - I could access the index.jsp page. Problems started when I added Spring to the project. (context, controller, etc.)
I'm using Eclipse Mars, Tomcat 8, maven 3.3.3 and Spring 4.2.2
Here is what I see in the log (probably only third line matters):
Looking for matching resources in directory tree [C:\Users\dima\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\cte\WEB-INF\classes\com\company\dept\demo\cte\controller]
Searching directory [C:\Users\dima\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\cte\WEB-INF\classes\com\company\dept\demo\cte\controller] for files matching pattern [C:/Users/dima/spring_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/cte/WEB-INF/classes/com/company/dept/demo/cte/controller/**/*.class]
Resolved location pattern [classpath*:com/company/dept/demo/cte/controller/**/*.class] to resources []
Instead of something like
INFO: Mapped URL path ...
As a result I get 404 when trying to open the web page. These are the files:
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/ldap
http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<context:component-scan base-package="com.company.dept.demo.cte.controller" />
<mvc:annotation-driven/>
<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>
</beans>
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>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>
</web-app>
And here is my controller:
package com.company.dept.demo.cte.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
#RequestMapping("/pid")
public class PidController {
#RequestMapping(value="/hello")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView("pidHello");
mav.addObject("message", "hello world");
return mav;
}
}
When trying to access localhost:8080/cte/pid/hello 404 returned and the log says:
DispatcherServlet with name 'dispatcher' processing GET request for [/cte/pid/hello]
Looking up handler method for path /pid/hello
Did not find handler method for [/pid/hello]
No mapping found for HTTP request with URI [/cte/pid/hello] in DispatcherServlet with name 'dispatcher'
Thanks.
Update:
WAR file is correct but for some reason when it is deployed to Tomcat controller class isn't copied.
Is it your full web.xml , Because you have to add your dispatcher servlet reference to web.xml , Like below -
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Http Status 404- the rest controller is not mapped corectly? [duplicate]

I am starting with Spring Framework and want to do a HelloWorld with annotatios, I've made it work creating a controller and a view, basic hello work I guess; however, I want to use annotatios since I can not use SimpleFormController any more (deprecated).
The error I am getting is Estado HTTP 404 - /av/index.jsp
I am using Netbeans and I am basing the example on the basic template It provides. I have the following files, I am pretty sure it is a missconfiguration but I can't find anything that would help me so far. Thanks in advance.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="annotationHandlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1"/>
<property name="alwaysUseFullPath" value="true"/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:component-scan base-package="controller"/>
</beans>
indexController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
#Controller
public class IndexController {
#RequestMapping(value="/index.do", method= RequestMethod.GET)
public ModelAndView inicio (){
ModelAndView mv = new ModelAndView("index");
mv.addObject("usuario", "jaxkodex");
return mv;
}
}
You are missing
<mvc:annotation-driven />
in your config dispatcher-servlet.xml
See here for more info
You mapped the path /index.do in your controller, so you have to access it with the following url: http://localhost/av/index.do
<mvc:annotation-driven/> will solve out your problem.
Please try this
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
add an entry in dispatcher-servlet.xml
in your disapatcher-servlet.xml, add
<mvc:annotation-driven/> to enable annotation driven handler mapping.
Try to go through one of my tutorials:
Creation of a simple Controller with java-based config
I hope I will get success with it

spring annotation not working

I am starting with Spring Framework and want to do a HelloWorld with annotatios, I've made it work creating a controller and a view, basic hello work I guess; however, I want to use annotatios since I can not use SimpleFormController any more (deprecated).
The error I am getting is Estado HTTP 404 - /av/index.jsp
I am using Netbeans and I am basing the example on the basic template It provides. I have the following files, I am pretty sure it is a missconfiguration but I can't find anything that would help me so far. Thanks in advance.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="annotationHandlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1"/>
<property name="alwaysUseFullPath" value="true"/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:component-scan base-package="controller"/>
</beans>
indexController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
#Controller
public class IndexController {
#RequestMapping(value="/index.do", method= RequestMethod.GET)
public ModelAndView inicio (){
ModelAndView mv = new ModelAndView("index");
mv.addObject("usuario", "jaxkodex");
return mv;
}
}
You are missing
<mvc:annotation-driven />
in your config dispatcher-servlet.xml
See here for more info
You mapped the path /index.do in your controller, so you have to access it with the following url: http://localhost/av/index.do
<mvc:annotation-driven/> will solve out your problem.
Please try this
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
add an entry in dispatcher-servlet.xml
in your disapatcher-servlet.xml, add
<mvc:annotation-driven/> to enable annotation driven handler mapping.
Try to go through one of my tutorials:
Creation of a simple Controller with java-based config
I hope I will get success with it

404 Error: "The requested resource /Controller is not available"

I have setup Spring MVC 3.0 & Hibernate on Apache Tomcat and got the application to launch without any errors.
However I’m available to route requests from my (welcome file) redirect.jsp to the home controller (/Home).
This is what is supposed to happen:
Welcome file redirect.jsp sends request using <%response.sendRedirect(/Home)%>
My home controller (/home) returns the view index which is in WEB-INF/views
This is my web.xml: I mapped the home controller (/Home) via annotation and inweb.xml however its still not being found.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Home</servlet-name>
<servlet-class>com.app.controller.spring.HomeController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Home</servlet-name>
<url-pattern>/Home</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
This is my application-context.xml's snippet:
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />-->
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans. For example #Controller and #Service. Make sure to set the correct base-package-->
<context:component-scan base-package="com.yourmarketnet.mvc" />
<!-- Configures the annotation-driven Spring MVC Controller programming model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
<!-- mapping of static resources-->
<mvc:resources mapping="/resources/**" location="/resources/" />
<import resource="hibernate-context.xml" /
>
This is my app-servlet:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:viewClass="org.springframework.web.servlet.view.JstlView" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
However I’m getting a 404 Error,
The requested resource (/Home) is not available.
What I would actually like to do is to remove the redirect.jsp, have the application go to \Home controller on launch/startup, and the \Home controller returns the index view or any other view.
The first thing I noticed is that in your web.xml, you call your config location applicationContext.xml, but you later described it as application-context.xml. Make sure it is actually named applicationContext.xml. Also, you can remove the "Home" servlet from your web.xml; this will be handled by Spring through your dispatcherServlet. Finally, in your SimpleUrlHandlerMapping, you define the mapping to be index.html, but you need to also define Home if you want requests to Home to be handled by indexController. Hope that helps.

Categories