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" %>
Related
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";
}
}
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" %>
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 !
I am migrating our application from struts 1.3 to spring 3.1.4 mvc. In the process i am also upgrading from tiles 1.1 to tiles 2.2. Jstl version is 1.1. With Tiles 2.2, i am having an issue about using put-list-attribute. I am trying to show a simple jsp page which should does following:
a) Header : include bunch of css (i commented it out as it doesn't work. Using firebug i see error "NetworkError: 404 Not Found - http://localhost:8080/appname/%BeachStyle%7d" --- {eachStyle}
No idea where %BeachStyle% came from.
b) Header : include title -- Works fine
c) body : show static text
tiles.xml
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name=".login" preparer="com.tiles.LoginController"
template="/tiles/layouts/layoutmain.jsp">
<put-attribute name="pageTitle" value="vivi test" />
<put-list-attribute name="baseStylesTest">
<add-attribute value="/styles/css/grids.css"/>
<add-attribute value="/styles/css/superfish.css"/>
<add-attribute value="/styles/css/styles.css"/>
</put-list-attribute>
</definition>
</tiles-definitions>
layoutmain.jsp
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<tiles:useAttribute id="stylesList" name="baseStylesTest" classname="java.util.List"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%-- <c:forEach var="eachStyle" items="${baseStylesTest}">
<link type="text/css" rel="stylesheet" href="<c:out value='${eachStyle}'/>" /> --%>
<title><tiles:insertAttribute name="pageTitle" /> </title>
</head>
<body>
Login page body
</body>
</html>
I tried the following:
i) Checked the tiles2.2 dtd and example to use it - found similar usage
ii) Checked examples over internet and how it's being used - found similar usage
iii) Checked how its used with struts -- found they use #attr.xxx as it is in pagescope
I don't see anything wrong in my implementation. The title attribute is rendered properly but on the attribute belonging to put-list-attribute.
Any help is much appreciated.
Found the solution. JSTL wasn't working as i declared my web-app version to be 2.5. This is not compatible with tomcat5.5 which i was using. Changed the web-app version to be 2.4 and all is good.
<?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">
---
</web-app>
I'm using freemarker, SiteMesh and Spring framework.
For the pages I use ${requestContext.getMessage()} to get the message from message.properties. But for the decorators this doesn't work. How should I do to get the internationalization working for sitemesh?
You have to use the fmt taglib.
First, add the taglib for sitemesh and fmt on the fisrt line of the decorator.
<%# taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%# taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%# taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<fmt:setBundle basename="messages" />
In my example, the i18n file is messages.properties. Then you need to use the fmt tag to use the mesages.
<fmt:message key="key_of_message" />
If you prefer templates and the freemarker servlet instead you can enter the following in your templates:
<#assign fmt=JspTaglibs["http://java.sun.com/jstl/fmt"]>
<#fmt.message key="webapp.name" />
and in your web.xml:
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>messages</param-value>
</context-param>