Im getting a ClassCastException if i use Attributes in my Custom Headline Tag. Without Attributes rendering works fine.
Calling <t:headline value="test" /> gives a ClassCastException even before a Method in my HeadlineComponent or HeadlineTag-Class is called. <t:headline /> works fine.
I'm using MyFaces-1.2, on BEA 10.3
default.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%# taglib prefix="t" uri="http://www.tobi.de/taglibrary" %>
<!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">
<title>Tobi Test</title>
</head>
<body>
<f:view>
<t:headline value="test" />
</f:view>
</body>
</html>
HeadlineComponent.java
package tobi.web.component.headline;
import java.io.IOException;
import javax.el.ValueExpression;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
public class HeadlineComponent extends UIOutput {
private String value;
private Integer size;
#Override
public Object saveState(FacesContext context) {
Object values[] = new Object[3];
values[0] = super.saveState(context);
values[1] = value;
values[2] = size;
return ((Object)(values));
}
#Override
public void restoreState(FacesContext context, Object state) {
Object values[] = (Object[])state;
super.restoreState(context, values[0]);
value = (String)values[1];
size = (Integer)values[2];
}
#Override
public void encodeBegin(FacesContext context) throws IOException {
// Wenn keine Groesse angegeben wurde default 3
String htmlTag = (size == null) ? "h3" : "h"+getSize().toString();
ResponseWriter writer = context.getResponseWriter();
writer.startElement(htmlTag, this);
if(value == null) {
writer.write("");
} else {
writer.write(value);
}
writer.endElement(htmlTag);
writer.flush();
}
public String getValue() {
if(value != null) {
return value;
}
ValueExpression ve = getValueExpression("value");
if(ve != null) {
return (String)ve.getValue(getFacesContext().getELContext());
}
return null;
}
public void setValue(String value) {
this.value = value;
}
public Integer getSize() {
if(size != null) {
return size;
}
ValueExpression ve = getValueExpression("size");
if(ve != null) {
return (Integer)ve.getValue(getFacesContext().getELContext());
}
return null;
}
public void setSize(Integer size) {
if(size>6) size = 6;
if(size<1) size = 1;
this.size = size;
}
}
HeadlineTag.java
package tobi.web.component.headline;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentELTag;
public class HeadlineTag extends UIComponentELTag {
private ValueExpression value;
private ValueExpression size;
#Override
public String getComponentType() {
return "tobi.headline";
}
#Override
public String getRendererType() {
// null, da wir hier keinen eigenen Render benutzen
return null;
}
protected void setProperties(UIComponent component) {
super.setProperties(component);
HeadlineComponent headline = (HeadlineComponent)component;
if(value != null) {
if(value.isLiteralText()) {
headline.getAttributes().put("value", value.getExpressionString());
} else {
headline.setValueExpression("value", value);
}
}
if(size != null) {
if(size.isLiteralText()) {
headline.getAttributes().put("size", size.getExpressionString());
} else {
headline.setValueExpression("size", size);
}
}
}
#Override
public void release() {
super.release();
this.value = null;
this.size = null;
}
public ValueExpression getValue() {
return value;
}
public void setValue(ValueExpression value) {
this.value = value;
}
public ValueExpression getSize() {
return size;
}
public void setSize(ValueExpression size) {
this.size = size;
}
}
taglibrary.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
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-jsptaglibrary_2_1.xsd"
version="2.1">
<description>Tobi Webclient Taglibrary</description>
<tlib-version>1.0</tlib-version>
<short-name>tobi-taglibrary</short-name>
<uri>http://www.tobi.de/taglibrary</uri>
<tag>
<description>Eine Überschrift im HTML-Stil</description>
<name>headline</name>
<tag-class>tobi.web.component.headline.HeadlineTag</tag-class>
<body-content>empty</body-content>
<attribute>
<description>Der Text der Überschrift</description>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>Die Größe der Überschrift nach HTML (h1 - h6)</description>
<name>size</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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-facesconfig_1_2.xsd"
version="1.2">
<component>
<description>Erzeugt eine Überschrift nach HTML-Stil</description>
<display-name>headline</display-name>
<component-type>tobi.headline</component-type>
<component-class>tobi.web.component.headline.HeadlineComponent</component-class>
<attribute>
<attribute-name>value</attribute-name>
<attribute-class>java.lang.String</attribute-class>
</attribute>
<attribute>
<attribute-name>size</attribute-name>
<attribute-class>java.lang.Integer</attribute-class>
<default-value>3</default-value>
</attribute>
</component>
</faces-config>
exception
Root cause of ServletException.
java.lang.ClassCastException: java.lang.String
at jsp_servlet._jsf.__default._jsp__tag1(__default.java:194)
at jsp_servlet._jsf.__default._jsp__tag0(__default.java:145)
at jsp_servlet._jsf.__default._jspService(__default.java:104)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.ServletStubImpl.onAddToMapException(ServletStubImpl.java:408)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:318)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:505)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:251)
at org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:341)
at org.apache.myfaces.application.jsp.JspViewHandlerImpl.buildView(JspViewHandlerImpl.java:486)
at org.apache.myfaces.application.jsp.JspViewHandlerImpl.renderView(JspViewHandlerImpl.java:337)
at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:41)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:140)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:182)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3498)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(Unknown Source)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>tobi.web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>org.apache.myfaces.webapp.MyFacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
</web-app>
I see, the <rtexprvalue> in taglibrary.tld should be replaced by <deferred-value> for JSF 1.2 on JSP 2.1 when you use UIComponentELTag.
<attribute>
<description>Der Text der Überschrift</description>
<name>value</name>
<required>false</required>
<deferred-value>
<type>java.lang.String</type>
</deferred-value>
</attribute>
<attribute>
<description>Die Größe der Überschrift nach HTML (h1 - h6)</description>
<name>size</name>
<required>false</required>
<deferred-value>
<type>java.lang.Integer</type>
</deferred-value>
</attribute>
Give it a try. Also see the JSF 1.2 release notes.
Related
This question already has answers here:
How to install and use CDI on Tomcat?
(2 answers)
Closed 4 years ago.
I started to test JSF 2.3 a short time ago. But I can't get one of the most important features to work. The use of ManagedBeans. I tried a lot, using different servlet containers (Tomcat 8&9, Jetty 9.2).But nothing helped. Hope somebody can see my failure in the resources. It's frustrating. I debugged but the bean is never reached. The primefaces component works fine(the primefaces lib is not the reason). But I never get bean data. PS. I'm using myfaces but with mojarra I've got the same problem.
my bean:
import javax.enterprise.context.RequestScoped;
import javax.faces.annotation.FacesConfig;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import java.io.Serializable;
#Named(value = "sampleBean")
#RequestScoped
#FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class SampleBean implements Serializable {
private String specTitle;
private String specVersion;
private String implTitle;
private String implVersion;
public SampleBean() {
Package facesPackage = FacesContext.class.getPackage();
specVersion = facesPackage.getSpecificationVersion();
specTitle = facesPackage.getSpecificationTitle();
implTitle = facesPackage.getImplementationTitle();
implVersion = facesPackage.getImplementationVersion();
}
public String info() {
return "hello from sampleBean!";
}
public String getSpecTitle() {
return specTitle;
}
public String getSpecVersion() {
return specVersion;
}
public String getImplTitle() {
return implTitle;
}
public String getImplVersion() {
return implVersion;
}
}
my configuration bean:
import javax.faces.annotation.FacesConfig;
#FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class ConfigurationBean {
}
my facelet:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Hello JSF 2.3</title>
<style>
.col-1 { text-align: right; }
.col-2 { font-weight: bold; }
</style>
</h:head>
<h:body>
<div style="border-style: dashed" class="ui-g">
<p:calendar mode="inline"/>
</div>
<div style="border-style: double">
<h3>Hello from JSF 2.3!</h3>
<h:panelGrid columns="2" columnClasses="col-1, col-2">
<h:outputText value="Specification:"/>
<h:outputText value="#{sampleBean.specTitle}"/>
<h:outputText value="Specification version:"/>
<h:outputText value="#{sampleBean.specVersion}"/>
<h:outputText value="Implementation:"/>
<h:outputText value="#{sampleBean.implTitle}"/>
<h:outputText value="Implementation version:"/>
<h:outputText value="#{sampleBean.implVersion}"/>
</h:panelGrid>
<p>CDI injection support: <b>#{sampleBean.facesContextValue}</b></p>
<p>Running on: <b>#{application.serverInfo}</b></p>
</div>
my web.xml:
<?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"><!-- Use web-app_4_0.xsd, version=4.0 after update to Java EE 8 -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.ENABLE_CDI_RESOLVER_CHAIN</param-name>
<param-value>truet</param-value>
</context-param>
<context-param>
<param-name>javax.faces.ENABLE_WEBSOCKET_ENDPOINT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.validator.ENABLE_VALIDATE_WHOLE_BEAN</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>0</param-value> <!-- No Cache -->
</context-param>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
my build.gradle:
plugins {
id 'war'
id 'org.gretty' version '2.2.0'
}
group 'de.danri'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
gretty{
//servletContainer="tomcat8"
//servletContainer="tomcat9"
}
dependencies {
// https://mvnrepository.com/artifact/org.apache.myfaces.core/myfaces-impl
compile group: 'org.apache.myfaces.core', name: 'myfaces-impl', version: '2.3.1'
// https://mvnrepository.com/artifact/org.apache.myfaces.core/myfaces-api
compile group: 'org.apache.myfaces.core', name: 'myfaces-api', version: '2.3.1'
// https://mvnrepository.com/artifact/org.primefaces/primefaces
compile group: 'org.primefaces', name: 'primefaces', version: '6.2'
// https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api
compile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2.1-b03'
// https://mvnrepository.com/artifact/javax.enterprise/cdi-api
compile group: 'javax.enterprise', name: 'cdi-api', version: '2.0.SP1'
// https://mvnrepository.com/artifact/javax.websocket/javax.websocket-api
compile group: 'javax.websocket', name: 'javax.websocket-api', version: '1.1'
// https://mvnrepository.com/artifact/javax.servlet/jstl
compile group: 'javax.servlet', name: 'jstl', version: '1.2'
// https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
// https://mvnrepository.com/artifact/javax.validation/validation-api
compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
faces-config.xml:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.3"
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-facesconfig_2_3.xsd">
</faces-config>
Thank your maress. Good adress! Solution:
I added the /META-INF/context.xml
<Context>
<Resource name="BeanManager"
auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>
I added weld-servlet shaded to my dependencies:
...
compile group: 'org.jboss.weld.servlet', name: 'weld-servlet-shaded', version: '3.0.5.Final'
...
Finally I used a local tomcat to run the artifact and not the gradle 'gretty' plugin.
It seems the plugin has problems with cdi.
I have issue with PrimeFaces 6.0 push, I have searched all questions here and no answer helped me. I'm building a internal email system and I'm trying to notify user if he has new email inserted in the DB based on back end event (observer pattern), here is my code:
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" version="3.1">
<display-name>Test Foo Email system</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- primeFaces push declaration -->
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.primefaces.push.PushServlet</servlet-class>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Push Servlet</servlet-name>
<url-pattern>/primepush/ *</url-pattern>
</servlet-mapping>
<!-- Ends here-->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/ *</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
My foo.xhtml page code sample
<script type="text/javascript">
function handleMessage(facesmessage) {
facesmessage.severity = 'info';
PF('growl').show([facesmessage]);
}
</script>
<div class="New_Message_container ">
<!-- primefaces Push component -->
<p:growl widgetVar="growl" showDetail="true" />
<p:socket onMessage="handleMessage" channel="/notify" />
</div>
Controller class:
private final static String CHANNEL = "/notify";
.
.
.
private void notifyView(NewEmailEvent evt) {
String CurrentUser = this.currentUser.getEmailID();
String EventUserId = evt.getUserID();
if (CurrentUser.equalsIgnoreCase(EventUserId)) {
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish(CHANNEL,
new FacesMessage("New Mail", "You have new Mail"));
}
}
NotifyResource class
import javax.faces.application.FacesMessage;
import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.impl.JSONEncoder;
#PushEndpoint("/notify")
public class NotifyResource {
#OnMessage(encoders = {JSONEncoder.class})
public FacesMessage onMessage(FacesMessage message) {
return message;
}
}
Included required libraries which i found was required for this code, I have downloaded these jars from the web which are used in my sample.
atmosphere-annotations-2.4.9.jar
atmosphere-compat-jbossweb-2.0.1.jar
atmosphere-compat-tomcat-2.0.1.jar
atmosphere-compat-tomcat7-2.0.1.jar
atmosphere-runtime-2.4.9.jar
primefaces-6.0.jar
slf4j-api-1.7.22.jar
slf4j-simple-1.7.22.jar
I have tired to debug my code and I noticed that the event bus it NULL when I try to use it (eventBus.publish()) so I got java.lang.NullPointerException.
<servlet-mapping>
<servlet-name>Push Servlet</servlet-name>
<url-pattern>/primepush/ *</url-pattern>
</servlet-mapping>
There is a space between /primepush/ and *, try removing it.
I have an login example, its a page where you go to Admin or Operator login...
Both of this, even with the correct credentials, end up on a servlet exception : login failed...
Here it is, hope you can help me:
Error Log:
javax.servlet.ServletException: Login failed
at org.apache.catalina.authenticator.AuthenticatorBase.doLogin(AuthenticatorBase.java:1083)
at org.apache.catalina.authenticator.AuthenticatorBase.login(AuthenticatorBase.java:1060)
at org.apache.catalina.connector.Request.login(Request.java:2692)
at org.apache.catalina.connector.RequestFacade.login(RequestFacade.java:1073)
at br.edu.unisep.bean.LoginBean.entrar(LoginBean.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:650)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
LoginBean.java
package br.edu.unisep.bean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
#ManagedBean
#RequestScoped
public class LoginBean {
private String login;
private String senha;
public String entrar(){
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
request.login(login, senha);
//VEREFICA SE O USUARIO LOGADO E POSSUI O PAPEL ADM
if (request.isUserInRole("ADMIN")){
return "admin/indexAdmin.jsf?faces-redirect=true";
} else {
return "sistema/indexSistema.jsf?faces-redirect=true";
}
} catch (ServletException e) {
e.printStackTrace();
return "login.jsf";
}
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" version="3.0">
<display-name>exemploAcesso</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>
/login.jsf
</form-login-page>
<form-error-page>
/erroLogin.jsf
</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin</web-resource-name>
<url-pattern>/admin/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>ADMIN</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Operador</web-resource-name>
<url-pattern>/sistema/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>OPERADOR</role-name>
</auth-constraint>
</security-constraint>
<error-page>
<error-code>403</error-code>
<location>/proibido.jsf</location>
</error-page>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
</web-app>
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="exemploAcesso">
<Realm className="org.apache.catalina.realm.JDBCRealm"
debug="4"
driverName="org.postgresql.Driver"
connectionURL="jdbc:postgresql://localhost:5434/controle_acesso"
connectionName="postgres"
connectionPassword="r4p4dur4"
userTable="usuarios"
userNameCol="id_usuario"
userCredCol="ds_senha"
userRoleTable="papeis_usuario"
roleNameCol="id_papel"
digest="MD5"
/>
</Context>
login.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
<meta charset="utf-8" />
<title>Exemplo Acesso</title>
</head>
<body>
<h:form>
Login:
<h:inputText value="#{loginBean.login}"/> <br />
Senha:
<h:inputSecret value="#{loginBean.senha}"/> <br />
<h:commandButton value="Entrar" action="#{loginBean.entrar}"/>
</h:form>
</body>
</html>
I'm using Tomcat built in functions to do it and I'm using a simple jsp that
posts the results from the form to 'j_security_check'. Whereby the fieldname used for the username is 'j_username' and for the password it is 'j_password' (I'm using Tomcat 8).
<form action="j_security_check" method="POST">
<table>
<tr>
<td><label for="username">Login:</label></td>
<td><input id="username" type="text" name="j_username" value=""></td>
</tr>
<tr>
<td><label for="password">Senha:</label></td>
<td><input id="password" type="password" name="j_password"></td>
<td><input type="submit" value="Login"/></td>
</tr>
</table>
</form>
I am currently working on a web project with Java EE and I recently decided to improve my coding thanks to frameworks Hibernate-Spring-JSF. However I encountered a problem. When I try to display the content of a managed bean, nothing is printed.
Here is the code of my managedBean :
#ManagedBean( name = "moviesBean" )
#SessionScoped
public class MoviesBean implements Serializable {
#Autowired
private transient MoviesService moviesService;
private List<Movies> moviesList;
private Movies movie;
#ManagedProperty( value = "test" )
private String genre;
#PostConstruct
public void init() {
movie = moviesService.getById( 3502914 );
System.out.println( "Id : " + movie.getImdbid() );
}
public List<Movies> getMoviesList() {
return moviesList;
}
public void setMoviesList( List<Movies> moviesList ) {
this.moviesList = moviesList;
}
public Movies getMovie() {
return movie;
}
public void setMovie( Movies movie ) {
this.movie = movie;
}
}
And the code of my jsp :
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!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>Movies detail</title>
</head>
<body>
<f:view>
<h:outputText value="#{moviesBean.genre }"/>
</f:view>
</body>
</html>
So, as you can see I just try to print the value of the field 'genre' of the bean MovieesBean, but all I have in return is a blank page. Am I doing something wrong ? Knowing that I have not any errors on the server side (I am using Tomcat 7). Here are the jars in the build path :
Build path libs
I googled this problem several times but didn't find any solution. Thanks in advance for your help !
Here is the code of my web.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>JavaServerFaces</display-name>
<!-- Add Support for Spring -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
You are not seeing genre because your expression is incorrect. Genre is Movies's attribute. Try instead:
<h:outputText value="#{moviesBean.movie.genre}"/>
i'm using Richfaces 3.3.4 final with JSF 1.2 on a SAP NetWeaver AS 7.31.
Now im facing some Issues with dynamically showing selected Nodes from a rich:tree in a h:outputText. The selectedNodeListener is invoked correctly and the Name of the Node is saved in a String, but my h:outputText only refreshes on Page Refresh and not on Selection like in the Example from the Showcases(http://showcase-rf3.richfaces.org/richfaces/tree.jsf?tab=model&cid=26981).
Does anyone know if this is a common Issue with the above Setup or am I just missing something? As far as I know SAP NetWeaver isnt in the List of supported Servers but its JEE5 compliant. Could this be the Reason?
I simplified the Example from the Richfaces Showcases to reproduce the Problem in a small Environment. Here is some Code:
SimpleTreeBean.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;
public class SimpleTreeBean {
private TreeNode rootNode = null;
private List<String> selectedNodeChildren = new ArrayList<String>();
private String nodeTitle;
private void loadTree() {
rootNode = new TreeNodeImpl();
TreeNodeImpl rt = new TreeNodeImpl();
rt.setData("Root");
rootNode.addChild(1, rt);
for(int i = 1; i <= 5; i++){
TreeNodeImpl child = new TreeNodeImpl();
child.setData("Child "+i);
rt.addChild(i, child);
}
}
public void processSelection(NodeSelectedEvent event) {
HtmlTree tree = (HtmlTree) event.getComponent();
nodeTitle = (String) tree.getRowData();
selectedNodeChildren.clear();
TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
if (currentNode.isLeaf()) {
selectedNodeChildren.add((String) currentNode.getData());
} else {
Iterator<Map.Entry<Object, TreeNode>> it = currentNode
.getChildren();
while (it != null && it.hasNext()) {
Map.Entry<Object, TreeNode> entry = it.next();
selectedNodeChildren.add(entry.getValue().getData().toString());
}
}
}
public TreeNode getTreeNode() {
if (rootNode == null) {
loadTree();
}
return rootNode;
}
public String getNodeTitle() {
return nodeTitle;
}
public void setNodeTitle(String nodeTitle) {
this.nodeTitle = nodeTitle;
}
}
test.jsp:
<%#taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%#taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%#taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%#taglib uri="http://richfaces.org/rich" prefix="rich"%>
<html>
<head>
<title>TestTree</title>
</head>
<body>
<f:view>
<h:form>
<h:panelGrid columns="2" width="100%" columnClasses="col1,col2">
<rich:tree style="width:300px" nodeSelectListener="#{simpleTreeBean.processSelection}"
reRender="selectedNode" ajaxSubmitSelection="true" switchType="client"
value="#{simpleTreeBean.treeNode}" var="item" ajaxKeys="#{null}">
</rich:tree>
<h:outputText escape="false" value="Selected Node: #{simpleTreeBean.nodeTitle}" id="selectedNode" />
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
Important Part of faces-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>simpleTreeBean</managed-bean-name>
<managed-bean-class>
com.realcore.web.beans.SimpleTreeBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
<</faces-config>/faces-config>
Additionally there are some Validators and Converters in my faces-config too. But I think this isnt important for this Problem.
And here is the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>SstSapWEB</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Making the RichFaces skin spread to standard HTML controls -->
<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</param-value>
</context-param>
<!-- Defining and mapping the RichFaces filter -->
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
This simple Example isnt working too.
I don't know how to fix or workaround this Issue. Any help is greatly appreciated.
Regards,
Daniel
just in fact somebody else has similar Problems: After working on other things in the mean time I now figured out how to solve this Issue.
It was rather easy and all work is done in the JSP:
Use Rich Tree Node inside the Rich Tree Tag
Use the ajaxSubmitSelection and reRender Attributes of the TreeNode
Wrap the Part of the Page, which should be rerendered in a <a4j:outputPanel/> and set its Attribute ajaxRendered to "true"
Ensure that the values of the id-Attribute of the <a4j:outputPanel/> and the reRender-attribute of the TreeNode are the same.
Hope this helps others with similar Problems,
Daniel