SpringMVC w/ Maven - not able to get Servlet to find second page - spring

I am trying to build a demonstrator example for a Spring MVC project that uses DispatcherServlet, following a precedent: https://www.youtube.com/watch?v=g2b-NbR48Jo&t=551s
It has been straightforward, up until I tried to add a second JSP file to the project and the program is not able to access it after initiating the main page successfully. Briefly, this project takes two numbers in the "index.jsp" file and then returns a plain text message in the "display.jsp" file, in advance of building an addition ability into the Controller...
I am also using Apache 10.0.2 for this and Spring Tools Suite 4.10.0.RELEASE with Eclipse.
This is the code for making the above image in the index.jsp file:
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
The display.jsp file simply is this, generated by Eclipse, and this is the file my program can't seem to find:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3c//DD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html14/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
I'm here
</body>
</html>
The web.xml file that initiates the DispatcherServlet and sets the URL pattern is also standard issue:
<!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>telusko</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The telusko-servlet.xml file has the expected content to facilitate annotation use in the controller:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.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-2.5.xsd ">
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.telusko"></ctx:component-scan>
</beans>
The AddController.java file:
package com.telusko;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//will be responsible for adding two numbers
#Controller
public class AddController {
#RequestMapping("/add")
public String add()
{
return "display.jsp";
}
}
POM file dependencies, but i don't think these connect to the issue:
Last thing, project structure:
The error I get after clicking the "Submit" button on the first page is this:
It is generally clear that Apache cannot find the page, but I cannot understand fully why. I've tried a number of resolutions to resolve the problem:
Eclipse is using the Tomcat installation in the server settings
The location is also set to Servers/Tomcat v10.0 at localhost.server
Tried changing the URL pattern to "/", "/Dispatcher/", to no avail, and left it back with just the forward slash
Deleted the "target" folder in the Maven file and let it recreate
Corrected header references in the XML file (i.e. adding a missed "/schema/")
Updated/cleaned Maven project and server, separately. It leads to a separate error :
And this error is solved with adding <load-on-startup>1</load-on-startup> under the set of tags, at which point it reverts to the original 404 error.
I am using later version of JAR files, but I'm not sure if it might be a compatibility issue with STS4 and the version in the project.
Running the "display.jsp" file on its own does make it show with the "I'm here" message.
Targeted Runtimes for the project is set to Apache Tomcat v10.0.
I am exploring other possible angles on this problem, but it has proven to be persistent. Thank you in advance for your insight!

Related

Simple Spring Web Project Not Working

I've been trying to learn how to develop a Spring Web application and can't seem to get it working. Using the Spring Tool Suite, I created a new Spring Project using the Simple Spring Web Maven template, which basically is comprised of four files:
index.jsp:
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<c:url value="/showMessage.html" var="messageUrl" />
Click to enter
</body>
</html>
/WEB-INF/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>spring-web-test</display-name>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<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>
/WEB-INF/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: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">
<!-- Uncomment and your base-package here:
<context:component-scan
base-package="org.springframework.samples.web"/> -->
<mvc:annotation-driven />
<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>
/WEB-INF/view/showMessage.jsp:
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
This all seemed simple and basic enough, so I built it using Maven and packaged it into a WAR file. I then deployed it to a local TomEE 7 server, went to http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT, and got to the very simple index.jsp page:
However, when clicking the link, which sends me to http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage.html, I get a 404 error from Tomcat:
I suspected it was because of the .html ending - based off what I understand of the dispatcher and view resolver, it would be looking for /WEB-INF/view/showMessage.html.jsp. However, trying to access http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage returns the same error. Looking at the Tomcat logs, I see
Nov 11, 2015 9:46:53 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage.html] in DispatcherServlet with name 'dispatcherServlet'
Nov 11, 2015 9:46:55 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage] in DispatcherServlet with name 'dispatcherServlet'
What am I doing wrong? The whole thing seems pretty simple, and I haven't even modified anything from the template Spring Tool Suite provides, so I'd have assumed it would work right out of the box. Unfortunately there doesn't seem to be a whole lot of resources online on developing Spring web applications for use in servlet containers, and of the ones I've found, nothing seemed to suggest I'm doing anything wrong.
Thanks.
Change your index.jsp to something like this
<c:url value="/showMessage" var="messageUrl" />
You don't need file extensions, because you've set them up in your mvc-config.xml.
Then you need a basic controller to handle the mapping. Something like this:
#Controller
public class WebController{
#RequestMapping(value = "/")
public String showMessage(){
return "index";
}
#RequestMapping(value = "/showMessage")
public String showMessage(){
return "showMessage";
}
}

spring mvc static resources failed to load

I am trying to use static resources in my sample spring mvc project, In my project I have created a theme under resources folder as shown in the picture.
Then I have added the following 3 lines of code in spring-mvc-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.sudheer.example" />
<context:annotation-config />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/theme/" cache-period="31556926"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
</beans>
WhenI try to access the static resources in many differnt ways,I am not able to access it.
This is my jsp page:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="/resources/themes/css/bootstrap.min.css" rel="stylesheet" >
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/themes/bootstrap.min.css">
<link href="<c:url value="/resources/themes/css/bootstrap.min.css"/>"
rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>1. Test CSS</h1>
<h2>2. Test JS</h2>
<div class="btn"></div>
</body>
</html>
And this 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>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Can any one help me to solve this issue?
You have 3 problems:
1: In your Maven project, the folder src/main/resources has a special meaning for Maven: its content will be put in the classpath after compiling.
2: when you use spring resources mapping then the part of the url that mapping that match the mapping part '**' will been added to the location part for obtaining the file. (sorry for the bad explanation) An example will make it more clear: assume you have this resource mapping
<mvc:resources mapping="/a/**" location="/b/c/" >
And you request the url http://localhost/myApp/a/d then spring will look for the file at /b/c/d! - So when you send an request for ....../resources/themes/css/bootstrap.min.css Spring would search for: /resources/theme/themes/css/bootstrap.min.css
3: in spring config and folder you use theme but in the jsp you use themes
Solution for 1 would be: either you change the location spring resource mapping to pick up the resource from classpath by using classpath: prefix in location attribute or you put your files in an other folder: source/main/webapp/resources/theme (then the location parameter /resources/theme/) would map.
example with classpath fix for 1 and one possible fix for 2:
<mvc:resources mapping="/resources/theme/**"
location="classpath:/resources/theme/" cache-period="31556926"/>
jsp - fix for 3:
<link href="<c:url value="/resources/theme/css/bootstrap.min.css"/>"
rel="stylesheet" type="text/css"/>
Probably is this:
You have this configuration
<mvc:resources mapping="/resources/**" location="/resources/theme/"
But when you call the resourcek, you use this
<c:url value="/resources/themes/css/bootstrap.min.css"/
i htink you have to use this
/resources/css/bootstrap.min.css
If a resource need visible by a frontend, you need put the resources in "webapp" folder. Example: webapp/theme/css/bootstrap.min.css
I have create a skeleton project for Spring MVC (static resources are used also) here.

JSTL tag is not showing its value on View page [duplicate]

There's a small problem with my servlets/jsp web application. I'm trying to use jstl in jsp page. When I use any tag for example:
<c:out value="${command}"/>
it shows me
${command}
in my browser instead of parameter 'command' value. I'm using maven (and I guess the problem is here). Here is pom xml dependencies:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
my web.xml declaration tag:
<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_3_0.xsd"
version="3.0">
and jsp part:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Parsing results</title>
<link type="text/css" rel="stylesheet" href="css/page.css"/>
<link type="text/css" rel="stylesheet" href="css/table.css"/>
</head>
<body>
<h2 align="center">Results of
parsing. Parsing method = <c:out value="${command}"/></h2>.......
EDIT:
Code, which sets command value, is simple:
request.setAttribute("command", parser.getName());
then goes
request.getRequestDispatcher(redir).forward(request, response);
Tell me please, what I'm doing wrong!
Thx!
Yes, i have doctype in web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "java.sun.com/dtd/web-app_2_3.dtd"; >
Remove that <!DOCTYPE> from web.xml and make sure that the <web-app> is declared conform Servlet 2.4 or newer and all should be well.
A valid Servlet 3.0 (Tomcat 7, JBoss AS 6-7, GlassFish 3, etc) compatible web.xml look like below in its entirety, without any <!DOCTYPE>:
<?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_3_0.xsd"
version="3.0">
<!-- Config here. -->
</web-app>
For Servlet 3.1 (Tomcat 8, WildFly 8-11, GlassFish/Payara 4, etc) it look like below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- Config here. -->
</web-app>
For Servlet 4.0 (Tomcat 9, WildFly 12-21, GlassFish/Payara 5, etc) it look like below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- Config here. -->
</web-app>
For Servlet 5.0 (Tomcat 10, WildFly 22-26, GlassFish/Payara 6, etc) it look like below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<!-- Config here. -->
</web-app>
When using JSTL 1.1 or newer, you need to assure that your web.xml is declared in such way that the webapp runs in at least Servlet 2.4 modus, otherwise EL expressions won't work in the webapp.
When still having a Servlet 2.3 or older <!DOCTYPE> or <web-app> in web.xml, even though you already have a Servlet 2.4 or newer XSD, then it would still be forced to run in Servlet 2.3 or older modus, causing the EL expressions to fail.
The technical reason is, EL was originally part of JSTL 1.0 and not available in Servlet 2.3 / JSP 1.2 and older. In JSTL 1.1, EL was removed from JSTL and integrated in JSP 2.0, which goes along with Servlet 2.4. So, if your web.xml is declared to run the webapp in Servlet 2.3 or older modus, then JSP would expect to find EL in JSTL library, but this would in turn fail if it's a newer JSTL version, lacking EL.
See also:
Difference between JSP EL, JSF EL and Unified EL - for a history of EL
Our JSTL wiki page
In my case for web.xml file (version="3.0") I had to run the application on Tomcat server v.8 instead of v.7, otherwise I had the same issue as you. Hope this helps someone...
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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_3_0.xsd"
version="3.0">
Setting <%# page isELIgnored="false" %> at the top of the page helped to me. Don't know why it was the root of the problem in my case. Not clear why yet.
Try placing the jdbc driver class in the WEB-INF -> lib folder and verfiy the versions of servlet and jar file used. In my case, I used mssql-jdbc-8.2.2.jar and update the same in pom.xml
My JSP page also couldn't recognize the <c:choose></:choose>. Always executed the false condition i.e <c:otherwise>. This is what was happening.
This was an inner JSP page i.e
<jsp:include page="your-inner-page.jsp"/>
The inner JSP was loading first and did not have the below tag libraries. They were placed on the outer JSP. Adding them to the inner worked for me.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

Spring MVC not initial request mapping

Does the web deploy mechanism affect Spring MVC work?
I create a web project(SpringMVCTest) contains web.xml and a pjsp pages then I create a Java project(SpringMVCTestSrc) and use the link source function to link test_src and SpringMVCTestSrc.
My Eclipse workspace
My problem is if I deploy the project in war file then it works fine
, EX: if I open
localhost:8080/SpringMVCTest/message/showMessage.pages
it will forward to
main/showMessage.jsp
but if I deploy the project with separate web and jar file it will no request mapping found like below.
[DEBUG]-[2015/07/30 15:03:39,o.s.w.s.DispatcherServlet(init):139]: Servlet 'dispatcher' configured successfully
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.DispatcherServlet(doService):861]: DispatcherServlet with name 'dispatcher' processing GET request for [/SpringMVCTest/message/showMessage.pages]
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.m.m.a.RequestMappingHandlerMapping(getHandlerInternal):294]: Looking up handler method for path /message/showMessage.pages
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.m.m.a.RequestMappingHandlerMapping(getHandlerInternal):302]: Did not find handler method for [/message/showMessage.pages]
[WARN]-[2015/07/30 15:03:40,o.s.w.s.PageNotFound(noHandlerFound):1136]: No mapping found for HTTP request with URI [/SpringMVCTest/message/showMessage.pages] in DispatcherServlet with name 'dispatcher'
The log shows the different.
If I deploy in war file. The log shows found 18 bean definitions and request mapping works fine.
Export war file from Eclipse and put it to apache-tomcat-8.0.21\webapps
Startup Tomcat and open localhost:8080/SpringMVCTest/message/showMessage.pages
The page will show Message : Hello world
o.s.c.a.ClassPathBeanDefinitionScanner(registerDefaultFilters):244]: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
o.s.c.i.s.PathMatchingResourcePatternResolver(doFindMatchingFileSystemResources):631]: Looking for matching resources in directory tree [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc]
o.s.c.i.s.PathMatchingResourcePatternResolver(doRetrieveMatchingFiles):693]: Searching directory [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc] for files matching pattern [D:/Temp/apache-tomcat-8.0.21/webapps/SpringMVCTest/WEB-INF/classes/in/hotkey/mvc/**/*.class]
o.s.c.i.s.PathMatchingResourcePatternResolver(findPathMatchingResources):424]: Resolved location pattern [classpath*:in/hotkey/mvc/**/*.class] to resources [file [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc\MainForm.class]]
o.s.c.a.ClassPathBeanDefinitionScanner(findCandidateComponents):286]: Identified candidate component class: file [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc\MainForm.class]
o.s.b.f.x.XmlBeanDefinitionReader(loadBeanDefinitions):224]: Loaded 18 bean definitions from location pattern [classpath:test-dispatcher-context.xml]
If I deploy with separate web and jar file. The log shows found 17 bean definitions and request mapping does not work.
Build a directory in apache-tomcat-8.0.21\webapps, EX: apache-tomcat-8.0.21\webapps\SpringMVCTest
Copy WEB-INF, META-INF from Eclipse workspace to apache-tomcat-8.0.21\webapps\SpringMVCTest
Export jar file from SpringMVCTestSrc and put it to apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\lib
Startup Tomcat and open localhost:8080/SpringMVCTest/message/showMessage.pages
The page shows HTTP 404
o.s.c.a.ClassPathBeanDefinitionScanner(registerDefaultFilters):244]: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
o.s.c.i.s.PathMatchingResourcePatternResolver(findPathMatchingResources):424]: Resolved location pattern [classpath*:in/hotkey/mvc/**/*.class] to resources []
o.s.b.f.x.XmlBeanDefinitionReader(loadBeanDefinitions):224]: Loaded 17 bean definitions from location pattern [classpath:test-dispatcher-context.xml]
Does someone know why the same code but different deploy mechanism has different result?
My code as blow
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>SpringMVCTest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:test-service-context.xml</param-value>
</context-param>
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:test-dispatcher-context.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.pages</url-pattern>
</servlet-mapping>
</web-app>
Spring Configuration(test-service-context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="testMessageService" class="in.hotkey.service.TestMessageService" >
</bean>
</beans>
Spring MVC Config(test-dispatcher-context.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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="in.hotkey.mvc"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
Main Controller
package in.hotkey.mvc;
import in.hotkey.service.TestMessageService;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class MainForm {
#RequestMapping(value="/message/showMessage.pages")
public ModelAndView handleShowMessage() throws Exception {
ModelAndView mv = new ModelAndView("main/showMessage");
mv.addObject("message", "Hello world!");
return mv;
}
}
JSP Content
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>
<h4>Message : ${message}</h4>
</body>
</html>
I found what the problem is....
Root cause founded from Spring MVC document.
It means annotation scan need directory entries!!
The problem is if I export whole project into war file. It contains the complete directory entries in WEB-INF/classes directory but jar file has no directory entry information.
Resolve this problem just check the "Add directory entries" option in Eclipse export function.

http status 404 hello.jsp (apache tomcat 6)

im new to using apache and spring framework and i've been using this tutorial for learning: http://docs.spring.io/docs/Spring-MVC-step-by-step/part1.html
i made an .jsp file however my browser tells me "The requested resource is not available."
the .jsp file is pretty simple
<html>
<head><title>Hello :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings.</p>
</body>
</html>
entering http://localhost:8080/project/hello.htm gives me a 404.
my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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" >
<servlet>
<servlet-name>project</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>project</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
any idea how to fix this?
404 error means the requested resource is not in the server.
Your problem can be either
1. Not properly matching hello.htm to any controller
bean id="/hello.htm" class="foo.controller" or
2. You have to put your hello.jsp in webcontent folder, along with index.jsp. Not in WEB-INF folder. Refer tutorial.

Resources