the message is not printed correctly on the view page in spring mvc [duplicate] - spring

This question already has answers here:
EL expressions not evaluated in JSP
(5 answers)
Closed 7 years ago.
JSTL variable values are not shown in EL. For example this code:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="s" uri="http://www.springframework.org/tags" %>
<html>
<body>
<c:forEach var="i" begin="1" end="5" >
<c:out value="${i}" />
</c:forEach>
</body>
</html>
browser renders like: ${i} ${i} ${i} ${i} ${i}
Or this one:
<c:set var="someVar" value="Hello"/>
<c:out value="${someVar}"/>
browser displays: ${someVar}
I'm using Spring-MVC 3 and Maven to build the sample project, deploying it to Tomcat 7.
In Spring's context I have view resolver configurated as follows:
<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="
org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp" />
</bean>
Model variables passed form Spring's controller not shown as well.
Mavens pom.xml has following jstl related dependencies:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
So, any suggestions how to resolve this?

So, the EL (those ${} things) doesn't get executed? That can happen when your servletcontainer runs at Servlet 2.3 / JSP 1.2 modus or lower, while you're using JSTL 1.1 or newer. During the change from JSTL 1.0 to 1.1, EL was moved from JSTL into JSP. That was JSP 2.0, which is part of Servlet 2.4. JSP 1.2 and older doesn't have EL bundled. JSTL 1.1 and newer doesn't have EL bundled.
You need to make sure that your web.xml root declaration conforms at least Servlet 2.4. As you're using JSP 2.1, which is part of Servlet 2.5, you're apparently targeting a Servlet 2.5 compatible container. So, make sure that your web.xml root declaration conforms Servlet 2.5:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
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_5.xsd"
version="2.5">
<!-- Config here. -->
</web-app>
Tomcat 7 is however a Servlet 3.0 compatible container. I'd consider changing maven pom to declare Servlet 3.0 / JSP 2.2 so that you can benefit all the new Servlet 3.0 features.
See also:
Our JSTL wiki page
History of EL

Related

java.lang.ClassNotFoundException: jakarta.servlet.http.HttpServlet : Spring MVC and Eclipse

I am creating a web application using Spring MVC and Eclipse IDE.
Spring Version- 6.0.3
To configure the project, I followed the following steps-
Added dependencies in pom.xml-
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring MVC Dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.3</version>
</dependency>
</dependencies>
Added Tomcat Server Runtime to the build path-
Added maven dependencies to deployment assembly-
web.xml- (in WEB-INF folder)
<web-app>
<display-name>Spring MVC Demo</display-name>
<!-- Configure dispatcher servlet -->
<servlet>
<servlet-name>dispatcherservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- / means handle all the requests from all urls -->
</web-app>
dispatcherservlet-servlet.xml (in WEB-INF folder)
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:p="http://springframework.org/schema/p">
<!-- Enable annotations -->
<context:component-scan base-package="spring-mvc-demo.src.main.java.controller"></context:component-scan>
<!-- View Resolver bean -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
name="viewResolver">
<!-- Inject two properties -->
<!-- Location for pages is given to prefix -->
<property name="prefix" value="/WEB-INF/views/" />
<!-- ending of page is .jsp -->
<property name="suffix" value=".jsp" />
<!-- Example name /WEB-INF/views/hello.jsp (here the name hello will be
given by controller) -->
</bean>
</beans>
Placed index.jsp under views folder in WEB-INF-
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h1>This is home page</h1>
<h1>Called by home controller</h1>
<h1>fired for /</h1>
</body>
</html>
Created HomeController.java class in src/main/java/controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/home")
public class HomeController {
#GetMapping("/current")
public String home() {
//return the name of the page
System.out.println("Hello this is home URL");
return "index";
}
}
I have created a controller with the url /home/current. On visiting this url I expect to see the desired index.jsp.
Problem-
When I "Run on Server" I get following error-
SEVERE: Allocate exception for servlet [dispatcherservlet]
java.lang.ClassNotFoundException: jakarta.servlet.http.HttpServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
Please help me to find the error in my configuration and the reason I get this error.
I saw several other posts, re-checked my steps but still getting the same error-
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
Based on the first screenshot in your question, you're using Tomcat 9. But for Spring 6 you need a minimum of Tomcat 10. Tomcat 10 is the first to use jakarta.* namespace whereas older versions used javax.* namespace.
Spring 6 (and Spring Boot 3) is the first version to use jakarta.* namespace whereas older versions used javax.* namespace.
So you have 2 options:
Upgrade Tomcat to a minimum of 10.
Or, downgrade Spring to a maximum of 5.
Clearly, option 1 is the recommended way to go in long term.
By the way, Spring 6 requires Java 17 not Java 1.7 as seen in the first screenshot in your question.
See also:
A Java 17 and Jakarta EE 9 baseline for Spring Framework 6
Apache Tomcat Versions
How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat?

Equivalent of s:property in Spring MVC

I'm currently converting a Struts2 project to Spring MVC and I'm struggling finding the equivalent of the s:property tag in Spring.
Here is some example of what I want to convert:
<s:property value="#contentha.idcontent"/>
<s:property value="%{topRex.idcontent}"/>
Thanks for your help !
You can write
${contentha.idcontent}
${topRew.idcontent}
If you want to use a tag you can write :
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value="${contentha.idcontent}" />
<c:out value="${topRew.idcontent}" default="Value if null" />
This will require the jstl dependency. For example if you use Maven :
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

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" %>

Tomcat 6.0.37 not processing jstl tag statements

I have a simple test page with the following statement but it appears that the ${} tags are not being processed by Tomcat and i get no errors in the log.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Test Example</title>
<body>
Setting value using c:out <c:set var="name" scope="request" value="Testing" /><br>
Value is: <b><c:out value="${name}"/></b><br>
</body>
</html>
Browser Output:
Setting value using c:out
Value is: ${name}
Using the information from https://stackoverflow.com/tags/jstl/info
I verified
1) Tomcat lib has jstl.2 jar , there are no duplicate jars - if i remove this jar i get a big exception complaining about missing tag classes.
2) My webapp does not have jstl jar in the classpath.
3) Tomcat web.xml has the correct servlet specification
<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">
Any help would be greatly appreciated.
Thanks
their may be conflict of jsp-api jar
remove jstl.version.jar from tomcat lib
and add directly dependency in your project
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
and try !

JspException and PageContext cannot be resolved

This is a follow up to a question on accessing resources in jsp page of spring mvc app
Thanks to #kmb385 I was able to resolve that problem but now I get the following eclipse errors in my JSP file
javax.servlet.jsp.JspException cannot be resolved to a type
and
javax.servlet.jsp.PageContext cannot be resolved to a type
as suggest by kmb385, here is my controller:
#Controller
public class AdminController {
#RequestMapping("/")
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("index");
model.addObject("msg", "hello world");
return model;
}
}
and here is my index.jsp page just in case:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<!-- <link type="text/css" rel="stylesheet" href="css/style.css"/> -->
<style type="text/css">
<%#include file="css/style.css" %>
</style>
<title>My Project - MainPage</title>
</head>
<body>
<h2 class="main_heading2">My Project - MainPage</h2>
<div style="float: right; width: 30%; text-align: center">
<p style="float:left;">an image should be here</p>
<img src="images/logo.jpg" alt="Logo"/>
<img src="${pageContext.servletContext.contextPath}/resources/images/logo.jpg" />
</div>
</body>
I have come across "solutions" to this by disabling it in the JSP validator but Please do not suggest this unless you can give a legitimate reason. I would rather correct this issue properly
Any Help appreciated
UPDATE:
Build path screen grab as requested by #kmb385
Try setting the servlet-api dependency in your pom.xml to provided. This jar maybe conflicting with the tomcat provided servlet-api.jar.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
Also be sure to include the jsp-api dependency, once again set as provided:
<!-- Servlet -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
Make sure all of your maven dependencies are being used to build the project by right clicking your project > Properties. On the deployment assembly tab click the add button, then Java Build Path Entries, then Maven Dependencies and finally Finish.
You may also need to add your maven dependencies to the build path. Right click your project > Maven > Update Project Configuration.
If you have downloaded all the dependencies in maven and the error is still not getting away, follow the steps below:
Right click on project and go to properties
Click over target run time
Check the box in front of the server you are using.
This should work.
Try import the class.
Modify your jsp's first line to look like this;
<%# page language="java" import="javax.servlet.jsp.PageContext" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
Add to the pom.xml dependencies:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
JSP:
Make sure to add on jsp, before tag:
<%# taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Get the context on JSP:
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
Import style css:
<link type="text/css" rel="stylesheet" href="${contextPath}/css/yourCssFile.css"/>
Dispatcher-servlet:
On your "spring-dispatcher-servlet.xml" add the following lines:
<beans xmlns="... xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="...
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:resources mapping="/resources/**" location="/resources/css/" />
Maybe, you need to add this adapters:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> [Optional]
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0"/>
</bean>
explained here: how to include js and css in jsp with spring MVC
How to solve javax.servlet.jsp.PageContext cannot be resolved to a type
1:- Select your project and Right click
2:- Go to Properties
3:- Click Targated Runtimes
4:- Check mark "Apache Tomcat v8.0"
I am using Apache v8.0 in my case
This will solve the problem
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
This alternative worked for me <%=request.getContextPath()%> which gets the Application context.

Resources