JSF RuntimeException: Cannot find FacesContext - java

When I write <h:outputText value="Login Name"/> tag in my JSP, I get the following exception message:
Cannot find FacesContext
Without that my JSP works fine. Here is my JSP:
<%# page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<body>
Login Name <input type="text" value=""/><br>
<h:outputText value="Login Name"/>
Password<input type="password" value=""/><br>
<input type="submit" value="Login">
</body>
</html>

There are two flaws in your code:
The root cause of this exception is that you forgot to pass the request through the url-pattern of the FacesServlet as definied in web.xml. If the JSP page is for example named page.jsp and the url-pattern of the FacesServlet is for example *.jsf, then you need to invoke it by http://example.com/context/page.jsf instead of .jsp. This way the FacesServlet will be invoked and create the FacesContext. Otherwise the JSF components in the page will complain that the FacesContext cannot be found and you will face this particular exception.
The <f:view> is missing in the page. Wrap the entire <html> in it. E.g.
<%# page pageEncoding="UTF-8" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!doctype ... >
<f:view>
<html>
...
</html>
</f:view>
By the way, that import attribute in the <%#page> is completely superfluous. Get rid of it.

Related

jsp page is not displaying the value of variable inside curly brackets just display the variable name

I am learning spring mvc and using jsp as my view layer but I have a problem with jsp page
when I put a an attribute in model like that
#RequestMapping
public String sayHello(ModelMap model)
{
model.addAttribute("message","welcome to 3-IDIOTS web store");
return "hello";
}
then inside the jsp page hello.jsp I try to use it like that
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<h2>${message}</h2>
<body>
</body>
</html>
then start to run tomcat and run the application I get this
this is what happens when I run tomcat
note: I know that I can solve it using the following code
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%
String message =(String) request.getAttribute("message");
%>
<html>
<head>
<title>Title</title>
</head>
<h2><%=message%></h2>
<body>
</body>
</html>
but I want to know why the code using ${meassage} is not working.
This syntax is el (expression language) must be explicit enabled.
In the jsp add this parameter isELIgnored=false
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8" isELIgnored="false" %>
Or add this in the web.xml
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>

org.apache.jasper.JasperException: The attribute prefix [fn] does not correspond to any imported tag library

I am trying to deploy and run the Hello World application using IntelliJIDEA 14.1.4
on Tomcat 8.0.14(Tomcat Server -> Local). The project was created using maven-archetype-webapp and later imported
to the IDE by specifying path to pom.xml.
Everything works fine as documented in the servlets tag info, but when I remove the following line:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
and add it in my include.jsp, I encounter an:
org.apache.jasper.JasperException: /WEB-INF/jsp/hello.jsp (line: 18, column: 42) The attribute prefix [fn] does not correspond to any imported tag library
Project structure:
include.jsp
<%-- 'header' file that will be included in every JSP page ensuring the same definitions are included in all our JSPs. --%>
<%# page session="false"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
index.jsp:
<%# include file="/WEB-INF/jsp/include.jsp" %>
<c:redirect url="/WEB-INF/jsp/hello.jsp"/>
hello.jsp:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Application Home</title>
<style>.error {
color: red;
}
.success {
color: green;
}</style>
</head>
<body>
<form action="hello" method="post">
<h1>Hello</h1>
<p>
<label for="name">What's your name?</label>
<input id="name" name="name" value="${fn:escapeXml(param.name)}">
<span class="error">${messages.name}</span>
</p>
<p>
<label for="age">What's your age?</label>
<input id="age" name="age" value="${fn:escapeXml(param.age)}">
<span class="error">${messages.age}</span>
</p>
<p>
<input type="submit">
<span class="success">${messages.success}</span>
</p>
</form>
</body>
</html>
Question:
Is it a bad idea to add the line only once(include.jsp) so that I can use it where required rather than adding it in every JSP or is the error cause because of something else which I am missing?
Silly, as it may seem, I missed the include statement in hello.jsp which was causing the exception. Posting the answer because I want to keep this information here for future reference.
Missing statement inside hello.jsp
<%# include file="/WEB-INF/jsp/include.jsp" %>
hello.jsp:
<%# include file="/WEB-INF/jsp/include.jsp" %>
<!DOCTYPE html>
....
</body>
</html>

jstl - unable to load class for jsp

When I try to add jstl tags into my jsp I get this:
HTTP Status 500 - org.apache.jasper.JasperException: Unable to load class for JSP
JSP may look like this:
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body>
<c:forEach items="${list}" var="post" >
<tr>
<td>${post.author}</td>
</tr>
</c:forEach>
</body>
</html>
I also tried to write
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
the result is the same.
Why does it happen?

Issue when deploying Struts app that runs on weblogic 8.1 to weblogic 11g

If I deploy an ear file on my local weblogic 8.1 server, it is working perfectly fine. But when I deploy it on Weblogic 11g, it gives an error.
Here is the scenario –
The first page of the application asks you to select the user. On user selection it will execute the RolesAction and take you to the roles page wherein the roles that are assiociated with the user will come as a drop down.
Once I deploy Argus application and select user on test login page, entire RolesAction class is getting executed but instead of getting page with roles associated to that user in the dropdown, I am getting “Error 404—Not Found” error page and in log file getting below mentioned error.
<Mar 20, 2011 8:20:42 PM GMT> <Error> <HTTP> <BEA-101017> <[ServletContext#406125315[app:ArgusDEV module:ArgusWeb path:/ArgusWeb spec-version:null]] Root cause of ServletException.
java.lang.NoSuchMethodError: org/apache/struts/config/ForwardConfig.getContextRelative()Z
at org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:298)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:232)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
Any inputs??
My Roles.jsp has the tld declaration as below:
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%# taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
Do you see any issue wrt the tld declaration?
Thoughts?
Thanks!
EDIT:
The first page that comes up in the application wherein you select the user is the Login.jsp. In this jsp, the tlds are declared as below:
This page is displyed. However, the roles.jsp page is the one that gives the error.
Login.jsp tld declaration:
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%# taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
Both the jsp's have the same tld declaration, and 1 gets displayed whereas the other one gives an error.
Ideas?
EDIT:
Roles.jsp
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%# taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link rel="stylesheet" type="text/css" href="css/arg.css" />
</head>
<form name="rolesForm" method="post">
<logic:present name="VIEW_BEAN" scope="request">
<logic:notEmpty name="VIEW_BEAN" property="userId" scope="request">
<input type="hidden" name="Id" value="<bean:write name="VIEW_BEAN" property="userId"/>">
</logic:notEmpty>
</logic:present>
<table border="0" cellpadding="1" cellspacing="0" width="95%" bgcolor="#FFFFFF">
<tr>
<td height="19"> </td>
</tr>
<tr>
<td width="100%" align="center" class="epi-dataTableLiteNew">
<font size="2"><b>Select a Role:</b> </font>
<select size="1" name="roleType" class="textbox" ">
<option value="">Select ---</option>
<logic:notEmpty name="VIEW_BEAN" property="roleList" scope="request">
<logic:iterate id="record" name="VIEW_BEAN" property="roleList" scope="request">
<option value="<bean:write name="record" property="roleID"/>"><bean:write name="record" property="roleName"/></b></option>
</logic:iterate>
</logic:notEmpty>
</select>
<input type="submit" value="Submit" onClick="return selectRole()" style="border:1px ridge #000000; height:22px; font-weight:bold cellpadding="0" cellspacing="0" 100%>
<p> </p>
<p> </p>
<p> </p>
</td>
</tr>
</table>
</form>
</body>
</html>
Login.jsp
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%# taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
</head>
<%# page
language="java"
%>
<meta http-equiv="Content-Type" content="text/html" />
<link rel="stylesheet" type="text/css" href="css/arg.css" />
<title>Home</title>
<%
String userid=request.getHeader("user");
String isLoginPage=request.getParameter("isUser");
%>
<!-- Please select the user from the list and press continue: -->
<form name="homePageForm" action="RolesAction.do" method="post">
</form>
</body>
</html>
EDIT:
struts-config.xml
<action
path="/roles"
name="HomePageForm"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request">
<forward name="success" path=".rolespage"/>
</action>
The action to be called for the url pattern is in the spring.xml file.
Checked the application lib folder to find struts-core-1.3.8.jar as well as struts.jar. It is because of this that the exception was thrown.
struts.jar has the forwardConfig class and getContextRelative() method.
struts-core-1.3.8.jar has the forwardConfig class , but not the getContextRelative() method.
This was causing the issue.
Therefore remove struts.jar so that 1.3.8 version is used.
-- Additionally add struts-extras-1.3.8.jar to the application lib
-- In the jsp, correct the tag lib uri to:
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%# taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
This solves the problem
Struts 1.1 <%# taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
Struts 1.2.x <%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
Please use above tag lib uri . Hope will solve your issue.

Please help with my JSP Internationalization problem

I have problem with I18N in JSP, specifically, with forms.
When I enter some Czech characters (e.g., "ěščřžýá...") into my page one form, into the field "fieldOne", and then show text from that field on page two, instead of Czech characters I see this as "ÄÄ". (Note, the second page gets the Czech characters with "request.getProperty("fieldOne")")
Here is the source code:
Page one:
<%#page contentType="text/html"%>
<%#page pageEncoding="UTF-8"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<html>
<head></head>
<body>
<form action="druha.jsp" method="post">
<input type="textarea" name="fieldOne">
<input type="submit">
</form>
</body>
</html>
Page two:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<html>
<head></head>
<body>
<h1>The text: </h1> <%=request.getProperty("fieldOne")%>
</body>
</html>
Thanks for help...
Which container are you using? This information is important for this kind of problems.
Anyway, try calling
request.setCharacterEncoding("UTF-8");
before reading the parameter. Sometimes setting the page encoding in the header directive isn't enough. You definitely need to do this in Tomcat and servlets, I assume that this could be also the case for JSPs.

Categories