Running Java Servlet cant view welcome page 404 error - java

Having an issue like this.
Error 404--Not Found
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.4.5 404 Not Found
web.xml
Here is my ..\MTSM\src\main\webapp\WEB-INF\web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<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>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ActionAdet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/ActionSure</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispacher-servlet.xml
Here is my ..\MTSM\src\main\webapp\WEB-INF\mvc-dispacher-servlet.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
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">
<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
hello.jsp
Here is my ..\MTSM\src\main\webapp\WEB-INF\pages\hello.jsp
$.get('ActionAdet', {opername: 'services'}, function (responseJson) { } ) ;..
hello.HelloController.java
Here is my ..\MTSM\src\main\java\com\springapp\mvc\HelloController.java
#Controller
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "ss";
}
}
ActionAdet.java
Here is my ..\MTSM\src\main\java\com\springapp\mvc\ActionAdet.java
#Controller
#RequestMapping("/ActionAdet")
public class ActionAdet extends HttpServlet { } etc..
I can't view my project in this url: localhost:7001/MTSM/
and also not available in: localhost:7001/MTSM/hello.jsp
It was working 2 days ago. I think I'm deleted something in web.xml, but I don't remember.
Do you know what problem is?
I uploaded my project here: [Dropbox -- MTSM_20140112.zip][3]
Thanks.

According to the logs you provided in pastie (that's bad : you should have providen the relevant part as an edit), spring tried to load WEB-INF/pages/ss.jsp and did not find it. I means :
you probably hit the controller HelloController
the method printWelcome returned ss as a string
due to mvc-dispacher-servlet.xml, InternalResourceViewResolver looked for WEB-INF/pages/ss.jsp (normal untill here)
as the file does not exist you got a 404 error (still normal : it is not what you expected, but what you asked)
You should try to be coherent between view names returned by controller and actual jsp file :
#RequestMapping("/")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello";
}
}
should forward to view WEB-INF/pages/hello.jsp (that exists according to you project image)

Related

Spring MVC handler mapping not working

I am very new to Spring MVC and have been following tutorials but they don't seem to be working.
I can load my index.jsp file fine but when I try to map it to the Dispatcher Servlet I keep getting resource not found errors.
HelloWorldController:
package com.javatpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC";
return new ModelAndView("hellopage", "message", message);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-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">
<context:component-scan base-package="com.javatpoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
my jsp files hellopage.jsp and welcomepage.jsp are in the folder jsp under my WEB-INF folder.
Index.jsp:
click|
click
I am trying to map hello.html to the HelloWorldController.java.
I would greatly appreciate any help as I am having problems mapping the first Spring MVC application.
The problem is with your servlet mapping, try changing it in your web.xml to:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
your url-pattern is set to *.html. Therefore only requests that ends with ".html" will be dispatched to spring mvc.
Add
<url-pattern>*.jsp</url-pattern>
to your servlet-mapping in web.js.
Can you try #RequestMapping(value="/hello",method = RequestMethod.GET ) ?
Your url pattern in the servlet-mapping is wrong.
You're saying go to the dispatcher servlet when the URL matches this:
*.html
So when you use "hello.html" the DispatcherServlet gets triggered.
However, there is no mapping for hello.html. There's only one for hello. HTML is static content, and it's not typical to serve that through a controller.
Try this:
Change the url-pattern to this:
<url-pattern>/</url-pattern>
Change the hello.html to hello.jsp
Change your method signature to return a string, and add the Model to the method.
#RequestMapping("/hello")
public String helloWorld(Model model) {
return "hello";
}
Your anchor tags should then point to the correct mapping. They'll look like this:
click|
So how does this work? These two pieces:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
The above is a pretty standard. In the servlet mapping, it's saying that anything that matches that URL pattern should hit the servlet named spring you defined in the web.xml.
The view resolver is looking in the /WEB-INF/jsp/ directory for files ending in .jsp. In other words, Spring will look for /WEB-INF/jsp/<string>.jsp. So when you return a String hello in the method, it is actually going to attempt to resolve /WEB-INF/jsp/hello.jsp.
If you understand the above, you may have realized you can serve *.html files this way also. They would require their own view resolver, however.

Spring servlet between jsp and html

I wanna know how to servlet call html and jsp file page.
I'm using spring mvc with servlet
when I run
localhost:8080/ -> it's running correctly
localhost:8080/htmlPage -> it's not working return error 404 not found
Here my path
Here my code
servlet mvc-dispatcher-servlet.xml
<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">
<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=""/>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:annotation-driven />
web.xml
<web-app 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>Spring MVC Application</display-name>
<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>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
HelloController.java
#Controller
public class HelloController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String wellcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello.jsp";
}
#RequestMapping(value="/htmlPage", method = RequestMethod.GET )
public String startHtml(){
return "hello.html";
}
}
<web-app 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>Spring MVC Application</display-name>
<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>mvc-dispatcher</servlet-name>
<url-pattern>*.so</url-pattern>
</servlet-mapping>
#Controller
public class HelloController {
#RequestMapping(value = "/home.so", method = RequestMethod.GET)
public String wellcome(ModelMap model) {
model.addAttribute("message", "Hello world!");
return "hello.jsp";
}
#RequestMapping(value="/htmlPage.so", method = RequestMethod.GET )
public String startHtml(){
return "hello.html";
}
}
please change your url pattern with ".so". all ".so" request are consider as spring request and handle by spring dispatcher servlet.
Your call is wrong. First there should be an app name and then request name.
You have to call like below :
localhost:8080/YourAppName/htmlPage

Error in navigating one page to another using Spring MVC framework?

I want to navigate from one page to another using spring MVC in my project.
I have two JSP page and one controller.
First page is welcome page which opens when i start the project using tomcat.
My project is able to show welcome page. But it can't find the mapping.
Welcome page code:
Say Hello
Second page code:
<body> ${message} </body>
Controller code:
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
System.out.println(message);
return new ModelAndView("hello", "message", message);
}
}
Servlet xml code:
<?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">
<context:component-scan
base-package="net.viralpatel.spring3.controller" />
<bean id="jspViewResolver" 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>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
</beans>
Web xml code:
<?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>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
Please help me in this.
#Controller
#RequestMapping("/helloWorld")
public class HelloWorldController {
#RequestMapping("/hello.html")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
System.out.println(message);
return new ModelAndView("hello", "message", message);
}
}
Now try hitting http://localhost:port/appName/helloWorld/hello.html if you see correct page then change the link as following
Say Hello

Spring MVC 3.0- The URL mapping is treated as a view when passing a model and view object

I am new to spring and was working on the Hello world application in MVC The URL mapping is searched as a corresponding view when i return the ModelAndView object from the controller..I have included all proper jars.This is the code..
/web-inf/sample-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"> (http://www.springframework.org/schema/context/spring-context-3.0.xsd%27%3E)
<context:component-scan base-package="com.tcs.laks.sample.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
This is the web.xml---
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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"> (http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%27%3E)
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>sample</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>/</url-pattern>
</servlet-mapping>
</web-app>
The HelloWorldController is..
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
#RequestMapping("/welcome")
public class HelloWorldController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){
String message = "Hello";
return new ModelAndView("hello", "message", message);
}
}
The url was given as //localhost:8080/sample/welcome it gave 404 as it was trying to find welcome.jsp instead of hello.jsp
HTTP Status 404 - /sample/WEB-INF/jsp/welcome.jsp
type Status report
message /sample/WEB-INF/jsp/welcome.jsp
description The requested resource (/sample/WEB-INF/jsp/welcome.jsp) is not available.
Specify the contextConfigLocation init-param for the dispatcher servlet within your web.xml file. The current configuration is even being read/used by the dispatcher
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/sample-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Also it looks like you have imported the portlet version of ModelAndView, make sure this is what you desire. If not import, org.springframework.web.servlet.ModelAndView
You are using wrong ModelAndView class
import org.springframework.web.portlet.ModelAndView;
it should have been
import org.springframework.web.servlet.ModelAndView;
everything else is fine.

Why spring warn message is" org.springframework.web.servlet.PageNotFound "?

I trying spring 3 mvc this package is
org.spring.test and code is
#Controller
#RequestMapping("/welcome")
public class WelcomeController {
private Logger logger = org.slf4j.LoggerFactory.getLogger(WelcomeController.class);
#RequestMapping(method = RequestMethod.GET)
public void welcome() {
logger.info("Welcome!");
}
#RequestMapping("test1")
public void test1() {
logger.info("test1!");
}
#RequestMapping("test2")
public void test2() {
logger.info("test2!");
}
}
web.xml is:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
last mvc.xml is
<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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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="org.spring.test" />
<!-- Configures support for #Controllers -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
When I use "mvn jetty:run" command and write to http://localhost:8080/springdemo/welcome
is
"HTTP ERROR 404
Problem accessing /springdemo/WEB-INF/views/welcome.jsp. Reason:
NOT_FOUND"
Console message is :
INFO : org.spring.test.WelcomeController - Welcome!
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/springdemo/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet'
Why?
It looks like you are missing /WEB-INF/views/test.jsp. Is that true?
You are requesting /test URI, which is not mapped to your controller class (which is mapped to /welcome, I think). Spring uses InternalResourceViewResolver to serve your request. It takes prefix + "test" + suffix. That is how you end up with /WEB-INF/views/test.jsp.
Make sure you have that JSP or change your controller mappings.
Update:
Ok, you changed the question. But do you have /springdemo/WEB-INF/views/welcome.jsp ?

Categories