HTML(5) Friendly Markup in JSF 2.2 Not working - html

I tried to reproduce the same example in this question using JSF 2.2.6 and Tomcat 7.0: JSF navigation rule doesn't work on form submit, I also read the JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output and respected all the recommandations provided by BalusC's answer, then I also consulted this question JSF 2 with HTML pages instead of XHTML because I want to use only .html files (I know i can use .xhtml but i need to understand the reason why this is not working).
I tried to make it simple as much as possible so any one could reproduce that:
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"
version="2.5">
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.html</param-value>
</context-param>
</web-app>
faces-config.xml (Navigation rules are not needed as it's provided dynamicly):
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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_2.xsd"
version="2.2">
<navigation-rule>
<display-name>index.html</display-name>
<from-view-id>/index.html</from-view-id>
<navigation-case>
<from-outcome>welcomePage</from-outcome>
<to-view-id>/welcome.html</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
BeanFilm.java:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class BeanFilm implements Serializable {
private String recherche = new String();
public String getRecherche() {
return recherche;
}
public void setRecherche(String recherche) {
this.recherche = recherche;
}
public String doRecherche() {
return "welcomePage";
}
}
index.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<head>
<title>Accueil</title>
</head>
<body jsf:id="body" >
<h1>Plain HTML5 with JSF</h1>
<form jsf:id="form">
<input type="text" jsf:id="recherche" jsf:value="#{beanFilm.recherche}"/>
<input type="submit" jsf:value="Submit" jsf:id="searchButton" jsf:action="#{beanFilm.doRecherche}"/>
</form>
<ui:debug/>
</body>
</html>
welcome.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<head>
<title>Accueil</title>
</head>
<body>
<h1>Welcome</h1>
</body>
</html>
The issue:
The exact problem is that when i click on the submit the method BeanFilm#doRecherch() is never called (using a breakpoint) and i can't really understand why? another information wich may be useful, is that in HTML code source attributes are still like jsf:id="searchButton"does this mean that the HTML wasn't generated?

Related

i am unable to see the login page on the web browser

this is the java code:i am not getting any error indication but the output of the code is not displayed on the web server.it is showing the web address as:(http://localhost:6027/HttpSearchBar/Example). Tell me whether the local host 6027 is a valid address?
package search.com;
import java.io.IO Exception;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Example extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 102831973239L;
public void dopost (HttpServletRequest hreq, HttpServletResponse hres)throws ServletException, IOException{
System.out.println("hello");
try {
hres.setContentType("text/html");
String s1 = hreq.getParameter("username");
String s2 = hreq.getParameter("password");
ServletContext sc = getServletContext();
if ((s1.equals("abc"))&&(s2.equals("xyz"))) {
hres.sendRedirect("welcome");
}else {
PrintWriter pw = hres.getWriter();
pw.print("invalid username/password");
RequestDispatcher rd = sc.getRequestDispatcher("login.html");
rd.include(hreq, hres);
}
} catch (Exception e) {
// TODO: handle exception
System.err.print(e);
}
} }
this is html code:
<!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>Example Html</title>
</head>
<body bgcolor=yellow text=blue>
<center>
<h1>
<u>LoginForm</u>
</h1>
<form action="Example" method="post">
UserName<input type="text" name="username">
Password<input type="text" name="Password">
<input type="submit" value="login" /><input type="reset">
</form>
</center>
</body>
</html>
this is web.xml code:
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HttpSearchBar</display-name>
<servlet>
<servlet-name>Example</servlet-name>
<servlet-class>Example</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/Example</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/Example</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/Example</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/Example</location>
</error-page>
<error-page>
<exception-type>java.io.IOException</exception-type>
<location>/Example</location>
</error-page>
<error-page>
<exception-type>java.lang. Throw able </exception-type>
<location>/search.com.Example</location>
</error-page>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
<welcome-file>login.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>
</web-app>
this is displayed when server is started:
Oct 22, 2018 6:41:52 PM org.apache.catalina.core.ApplicationDispatcher
invoke
WARNING: Servlet Example is currently unavailable
Send a request to http://localhost:6027/ from your browser. If you can see the default Apache Tomcat page the localhost:6027 is a valid address.
There are a couple of reasons that you might not see any response.
Java is case sensitive. Your do post method is incorrect. Replace with doPost
public void doPost(HttpServletRequest hreq, HttpServletResponse hres)
throws ServletException, IOException {
Your html password input name is name="Password", and you are getting it with lowercase p.
String s2 = hreq.getParameter("Password"); //uppercase P
Finally don't use RequestDispatcher, just redirect the page. Replace everything in your else statement.
hres.sendRedirect("login.html");
By referring to the server log that has attached, it seems that you are trying to run your servlet project on apache tomcat server. Default Tomcat port is 8080. That is you should try to access your running app on:
http://localhost:8080
if you want to customize the running port you have to do it using the server.xml file.

404 error on form submission in Java [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I'm developing simple web application. I've created Dynamic Web Project in Eclipse Mars and I'm using Java 1.8 and Tomcat v8.0.36.
I've created a simple form:
<!DOCTYPE html>
<html>
<head>
<title>Coffee Advice Page</title>
</head>
<body>
<form action=”SelectCoffee.do”>
Select Coffee characteristics<p>
Color:
<select name=”color” size=”1”>
<option value=”light”> light </option>
<option value=”amber”> amber </option>
<option value=”brown”> brown </option>
<option value=”dark”> dark </option>
</select>
</br></br>
<input type="submit" value="Submit">
</form>
</body>
</html>
A Servlet:
package com.example.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#SuppressWarnings("serial")
public class CoffeeSelectionServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Inside doGet()");
PrintWriter printWriter = response.getWriter();
printWriter.println("doGet() is working fine!");
}
}
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"
id="WebApp_ID" version="3.1">
<display-name>CoffeeAdvice</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>Servlet1</servlet-name>
<servlet-class>com.example.web.CoffeeSelectionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/SelectCoffee.do</url-pattern>
</servlet-mapping>
</web-app>
But when I start Tomcat server and submit this form, I get 404 Error with query string:
http://localhost:8080/CoffeeAdvice/%C3%A2%E2%82%AC%C2%9DSelectCoffee.do%C3%A2%E2%82%AC%C2%9D?%E2%80%9Dcolor%E2%80%9D=%E2%80%9Dlight%E2%80%9D
which is not I intended.
If I make GET request directly from browser bar:
http://localhost:8080/CoffeeAdvice/SelectCoffee.do?color=light
It works absolutely fine!
Please let me know why this query string is generated distorted like this and what I'd have to change.
Any help would be much appreciated!
The double quote sign which you are using in form action is not proper
I unescaped your url which is now
"http://localhost:8080/CoffeeAdvice/ââ¬ÂSelectCoffee.doââ¬Â?âcolorâ=âlightâ"
Fix your double quote.

Why won't my CSS link?

I can't seem to get my CSS to link to my page. I've tried multiple fixed already on the website and I've had a few people look at is but for some reason it just wont work. I've tried putting it in the Web-inf, and multiple difference ways in which too link the CSS.
JSP
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!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 href="${pageContext.request.contextPath}/resources/css/bootstrap.css" rel="stylesheet">
<title>Insert title here</title>
</head>
<body>
<div class="panel panel-default">Hello</div>
<p>${topAlbums.size() }</p>
<c:forEach items="${topAlbums}" var="album">
<p>${album}</p>
</c:forEach>
</body>
</html>
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_2_5.xsd" version="2.5">
<display-name>Spring Web App</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
dispatcher-servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
">
<context:component-scan base-package="com.springwebapp"> </context:component-scan>
<context:component-scan base-package="de.umass"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
Error:
http://i.imgur.com/1zd9tHL.png
Files:
http://i.imgur.com/yL8je5u.png
UPDATE:
Got this working, it was something in my dispatcher servlet blocking all CSS files. Thanks for all the help people :)

My Basic jsf tags are not rendering in to html tags

Im a Totally new in Java and JSF. I'm using eclipse Indigo and Tomcat 6.0.3 and JSF 2.0.
When i run the Page in Browser, I just get an empty page, but i could the elements in firebug that it is still in JSF tags itself. It is not rendering in html..
This is my 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>ContactFormJSF</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>pages/index.xhtml</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>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>*.xhtml</param-value>
</context-param>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
This is my Basic JSF content
<?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:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add New User Form</title>
</h:head>
<h:body>
<f:view>
<h:form>
<h:panelGrid border="1" columns="3">
<h:outputText value="Name"></h:outputText>
<h:inputText value="#{userBean.name}" required="true"></h:inputText>
<h:outputText value="D.O.B"></h:outputText>
<h:inputText id="DOB" value="#{userBean.dob}" required="true"> </h:inputText>
<h:outputText value="Age"></h:outputText>
<h:inputText id="age" value="#{userBean.age}" required="true"> </h:inputText>
<h:commandButton action="#{userBean.addUser}" value="Submit"></h:commandButton>
<input type="reset"/>
<h:commandButton action="#{userBean.reset}" value="Reset"> </h:commandButton>
</h:panelGrid>
</h:form>
</f:view>
</h:body>
</html>
Iam Struggling for this for a week, I have tried out many things noted down in stackoverflow..
The URL iam using is localhost:8080/ContactFormJSF/
I could see the HTML tags which is added above in the Browser but not the jsf tags..
Actually, the FacesServlet mapping in your web.xml doesn't cover the .xhtml files, so you should add the proper mapping to the configuration file:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
In other side, you should retreive this to let the JSF controller recognize the .xhtml files in your project:
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>*.xhtml</param-value>
</context-param>

How to create a custom EL function to invoke a static method?

Im new to JSF 2. My question is related to BalusC's answer to this question jsf2 ajax update parts based on request parameters I tried the kickstart code BalusC posted and I encountered an EL parsing error:
/nameofpage.xhtml #12,64 rendered="#{bean.panels.contains('u1')}"
Error Parsing: #{bean.panels.contains('u1')}
I guess that this is caused because I'm not running a Servlet 3.0 / EL 2.2 capable container with a /WEB-INF/web.xml declared as per Servlet 3.0 spec. I'm using Tomcat 6.
BalusC suggested in his answer to create a custom EL function. But how do I accomplish this using a custom EL function? Or can this be fixed by just configuring certain parts of my project?
Below is my 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" version="2.5">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
First create a final class with a public static method which does exactly the job you want:
package com.example;
import java.util.Collection;
public final class Functions {
private Functions() {
// Hide constructor.
}
public static boolean contains(Collection<Object> collection, Object item) {
return collection.contains(item);
}
}
Then define it as a facelet-taglib in /WEB-INF/functions.taglib.xml:
<?xml version="1.0" encoding="UTF-8"?>
<facelet-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-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://example.com/functions</namespace>
<function>
<function-name>contains</function-name>
<function-class>com.example.Functions</function-class>
<function-signature>boolean contains(java.util.Collection, java.lang.Object)</function-signature>
</function>
</facelet-taglib>
Then familarize Facelets with the new taglib in the existing /WEB-INF/web.xml:
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/functions.taglib.xml</param-value>
</context-param>
(note: if you already have the javax.faces.FACELETS_LIBRARIES definied, then you can just add the new path semicolon separated)
Then define it in the Facelets XHTML file as new XML namespace:
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:func="http://example.com/functions"
...
>
Finally you can use it as intended:
rendered="#{func:contains(bean.panels, 'u1')}"
As a completely different alternative, you can also include JBoss EL in your project. It works on Tomcat 6.0 and you'll be able to invoke non-getter methods in EL. Drop jboss-el.jar file in /WEB-INF/lib and add the following to your web.xml:
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
Since EL 2.2 there's another approach: create an #ApplicationScoped bean with methods in turn referring to those static functions. See also a.o. Utility methods in application scoped bean.