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!
I have added
<mvc:resources mapping="/webapp/**" location="/webapp/" />
<mvc:default-servlet-handler />
in my dispatch-servlet.xml
my folder hierarchy is
enter image description here
and in index.jsp i declared
<spring:url value="/webapp/resources/js/bootstrap.min.js" var="bootstrapJS" />
<script src ="${bootstrapJS}" type="text/javascript"></script>
can any one tell the actuall issue is with code or folder hierarchy
Hi have you included the tag at the top of index.jsp it will look something like this. <%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
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" %>
Got some trouble with using jstl in atg jsp page. Added
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %> and some library at /WEB-INF/lib (standard.jar, jstl.jar).
My web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
So <dsp:getvalueof param="id" var="prodId"/>
<c:set var="link" value="genericproduct.jsp?id=${prodId}"/> works fine and shows for example "genericproduct.jsp?id=prod180007",but ${prodId} shows nothing,at the moment <c:out value="${prodId}"/> works. And <dsp:setvalue bean="CustomFormHandler.errorURL" value="genericproduct.jsp?id=${prodId}"/> doesn't works - "genericproduct.jsp?id=${prodId}". Version of JSTL 1.0, application runs on jboss 4.0.5GA.I look forward to your reply.Thanks!
<%# page isELIgnored="false" %>
<%#taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
Here is the POM file
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>all-themes</artifactId>
Web.xml
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>aristo</param-value>
On the main page http://localhost/helloworld/search.faces primefacs theme loads fine
Generated Page with primefaces style sheet is
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml"><head><link type="text/css" rel="stylesheet" href="/helloworld/javax.faces.resource/theme.css.faces?ln=primefaces-aristo" /><link rel="stylesheet" media="screen" type="text/css" href="/helloworld/javax.faces.resource/primefaces.css.faces?ln=primefaces&v=5.0" /><script type="text/javascript" src="/helloworld/javax.faces.resource/jquery/jquery.js.faces?ln=primefaces&v=5.0"><!--
//--></script><script type="text/javascript" src="/helloworld/javax.faces.resource/primefaces.js.faces?ln=primefaces&v=5.0"><!--
//--></script><script type="text/javascript" src="/helloworld/javax.faces.resource/jquery/jquery-plugins.js.faces?ln=primefaces&v=5.0"><!--
//--></script>
but when I access subsequent pages which is one folder down
http://localhost/helloworld/app/welcome.faces, theme is not applied. Url generated for the theme file is adding the extra folder which is in the url and thus not able to find the theme file resulting in theme not getting loaded. What should I do for the extra folder to not get added to the for the primefaces theme url...
generated page is
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml"><head><link type="text/css" rel="stylesheet" href="/helloworld/app/javax.faces.resource/theme.css?ln=primefaces-aristo" /><link rel="stylesheet" media="screen" type="text/css" href="/helloworld/app/javax.faces.resource/primefaces.css?ln=primefaces&v=5.0" /><script type="text/javascript" src="/helloworld/app/javax.faces.resource/jquery/jquery.js?ln=primefaces&v=5.0"><!--
//--></script><script type="text/javascript" src="/helloworld/app/javax.faces.resource/jquery/jquery-plugins.js?ln=primefaces&v=5.0"><!--
//--></script><script type="text/javascript" src="/helloworld/app/javax.faces.resource/primefaces.js?ln=primefaces&v=5.0"><!--
//--></script>
code
There is no theme related code in xhtml... mentioned in web.xml and all- themes in POM file.
welcome.xhtml and search.xhtml are the same and here is the snippet
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/layouts/standard.xhtml">
<ui:define name="title">
</ui:define>
standard.xhtml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">....
Summary of the problem
If the url is /helloworld/search.faces generated in the page is /helloworld/javax.faces.resource/primefaces.js.faces?ln=primefaces&v=5.0" ... and theme loads fine.
When the url is ( is one folder deep) theme does not load /helloworld/app/welcome.faces path generated is in the page is href="/helloworld/app/javax.faces.resource/theme.css?ln=primefaces-aristo ... see the app directory that is added.... This is causing 404 erros for this resouce as it is not able to find it.
Is there a way to control the generated primefaces theme path ?
Upon further digging thro the rest of the config files. Added the following line to servlet context.
<!-- Enable processing of JSF 2 resource requests. For example: /webflow-primefaces-showcase/app/javax.faces.resource/jsf.js?ln=javax.faces -->
<faces:resources />
and now the theme is getting applied without any issues. Thanks for all those who chimed in.