Here is my problem, can someone please help me....!
Present, i am migrating an application from struts to JSF2.
We have a user validation link, which sent to their email for verification,
after their successful registration.
For example:
http://testserv:5050/bjb/page/validateAccount.faces?Email=qwerty#testsession#1.com&Code=1272
The email address contains special characters, in old links which were sent to users.
In new application we are encoding them using URLEncoder.encode.
i think, this is nothing to do with UTF-8, But however
i set request, response, and JSF ExternalContext#setResponseCharacterEncoding to UTF-8.
tomcat is also configured for UTF-8.
validateAccount.xhtml
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</h:head>
<h:body>
<h:form acceptcharset="UTF-8" name="verifyUserAccountForm" id="verifyUserAccountForm" prependId="false">
<f:metadata>
<f:event type="preRenderView" listener="#{verifyUserBean.execute}"></f:event>
</f:metadata>
</h:form>
</h:body>
</html>
In java code,
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(paramName);
Could not read the URL GET Request Parameters properly.
How should i solve this for existing links.
I have something in my mind like, i can write a javascript function in my xhtml page,
Which could be excuted on load, so using javascript function i can encode parameters.
But how to execute that javascript function before <f:event type="preRenderView"> call.
is there any better solution ?.
The query string parameter in the outgoing URL should be encoded as follows wherein the desired charset is explicitly mentioned:
String emailParam = URLEncoder.encode(email, "UTF-8");
String url = "http://testserv:5050/bjb/page/validateAccount.faces?Email=" + emailParam + "&Code=1272";
The Tomcat server should be confidured to decode the incoming URL using the very same charset:
<Context ... URIEncoding="UTF-8">
That's all you need to do with regard to encoding/decoding GET URLs. The HTML meta tag is irrelevant to this. The HTTP response encoding is irrelevant to this. The HTML form accept charset does more worse than good.
Please note that you edit the <Context> element in the right Tomcat configuration file. If you're for example managing Tomcat in Eclipse, you'd by default need to edit the one in Eclipse's Servers project, not in Tomcat's installation folder.
See also
Unicode - How to get the characters right?
Related
I am using PrimeFaces 3.4 in my web app and for a particular page the controls are not displayed with the normal PrimeFaces skin:
<!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"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>VMS login</title>
</h:head>
<h:body>
<h:form id="loginForm">
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
<p:panel header="#{msgs['login.title']}">
<p:panelGrid id="loginPanel" columns="2">
<h:outputText value="#{msgs['login.username']}" />
<p:inputText id="j_username" value="#{loginFormBean.userName}" required="true"></p:inputText>
<p:message for="j_username" ></p:message>
<h:outputText value="#{msgs['login.password']}" />
<p:password id="j_password" value="#{loginFormBean.password}" required="true" feedback="false"></p:password>
<p:message for="j_password"></p:message>
<p:commandButton action="#{loginController.loginUsingSpringAuthenticationManager}" value="#{msgs['login.button']}" update="loginForm" ajax="true"></p:commandButton>
</p:panelGrid>
</p:panel>
</h:form>
</h:body>
</html>
This outputs to:
The panel should have a header and so on.
The interesting thing is that in another page where I am using a <p:layout> with different panels in the layouts they display fine with their normal PrimeFaces look-and-feel.
What am I doing wrong? Thank you
Given that it only occurs on the login page, that can happen when the authentication mechanism also kicks on requests to JSF resources like CSS/JS/image files and redirects them to the login page as well. The webbrowser would then retrieve the HTML representation of the login page instead of the concrete resources. If you have investigated the HTTP traffic in the webbrowser's developer toolset, then you should have noticed that as well.
If you're using homegrown authentication with a servlet filter, then you need to tell the filter to not redirect them to the login page, but just continue them. It are those /javax.faces.resource/* URLs (you can get that URL path as constant by ResourceHandler#RESOURCE_IDENTIFIER).
if (request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
chain.doFilter(request, response);
return;
}
// ...
Or if you're using container managed authentication, then you should add /javax.faces.resource/* to allowed URLs which should be skipped from login checks:
<security-constraint>
<web-resource-collection>
<web-resource-name>Allowed resources</web-resource-name>
<url-pattern>/javax.faces.resource/*</url-pattern>
</web-resource-collection>
<!-- No Auth Contraint! -->
</security-constraint>
See also Exclude css & image resources in web.xml Security Constraint.
Or when you're using 3rd party authentication framework like Spring Security, then you need to tell it the following way (assuming 3.1.0 or newer)
<http security="none" pattern="/javax.faces.resource/**" />
See also Spring Security 3.0.5.
Or when you're using PicketLink, see PrimeFaces based application with PicketLink does not show style in login page.
I want to invoke one method through a link from Facelets:
My Facelets code is like:
<h:commandButton value="A" actionListener="#{customerData.searchedCustomerListA}" />
<h:commandLink value="A" actionListener="#{customerData.searchedCustomerListA}"/>
Backing bean code is like:
public void searchedCustomerListA(ActionEvent ae){
customerName = "A";
leftCustomerListAvailable.clear();
if(customerDataBean.getSearchedCustomerList(customerName)!= null)
leftCustomerListAvailable =customerDataBean.getSearchedCustomerList("A");
}
The same code is working for <h:commandButton> but not working for <h:commandLink>. How is this caused and how can I solve it?
The technical difference between <h:commandLink> and <h:commandButton> is that the link uses JavaScript to submit the parent form. So if it doesn't work while a syntactically equivalent button works fine, then that can only mean that either JavaScript is disabled in browser, or that the jsf.js file containing the mandatory helper functions isn't included in the page (which you should easily have noticed by seeing JS errors in the JS console of browser's builtin developer toolset).
So, to fix this problem, you need to verify if JS is enabled in browser and that you've a <h:head> component instead of plain HTML <head> in the template, so that JSF will be able to auto-include the jsf.js file.
Or, if your application's business requirements requires that the application functions as designed with JS disabled, then you should stick to <h:commandButton> and throw in some CSS to make it to look like a link (e.g. remove background, padding, border, inset, etc).
Try this, This sould work.
<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"
xmlns:p="http://primefaces.org/ui">
<h:form>
<h:commandLink type="button" action="#{testBean.tsetLink}">
<h:outputText value="A" />
</h:commandLink>
</h:form>
</html>
ManagedBean
#ManagedBean
#RequestScoped
public class TestBean {
public void tsetLink(){
System.out.println("Link clicked!!!!!!!!!!!!");
}
}
I my case the cause of this issue was a poorly configured url rewriting filter. One of the filters patterns unintentionally matched http://localhost:8080/mysite/javax.faces.resource/jsf.js.xhtml?ln=javax.faces which prevented jsf.js from being loaded. Check this answer: Clicking h:commandLink causes Uncaught ReferenceError: mojarra is not defined.
I have the following problem, We have web content manager (WCM) running at remote host,
which is responsible for generating header and footer HTML files.
i.e. header.html, footer.html.
The HTML files are not properly formatted syntax wise,
WCM generated files have
Space character ( ) 🡢 it is not allowed in XHTML.
Non Closing break line (<br>) tags 🡢 it is invalid in XHTML.
So the WCM generated HTML pages might not be valid XHTML pages.
We are implementing some of our applications in JSF,
where we need to include the WCM generated header and footer files.
Can we include the non-formatted HTML files into our XHTML files?
commonTemplate.xhtml
<html>
<head>
..........;
</head>
<body>
<ui:include src="remote_host/header.html" />
<ui:insert name="commonBodyContent" />
<ui:include src="remote_host/footer.html" />
</body>
</html>
I guess it is related to this question: Include non-Facelet content in a Facelet template
I do not recommend to mix XHTML with HTML, but most probably the browsers will not have any issues with the mentioned characters, hence you might try to directly render the file contenty, e.g. by
<h:outputText value="#{yourBean.headerCode}" escape="false" />
Whereas YourBean.getHeaderCode() would readout the header file's content and return it as String. YourBean should be ApplicationScoped.
Faster and better would be to get the WCM generating valid XHTML.
I tried to pass UTF-8 String as f:viewParam value but value shown as garbage string, i added EncodingFilter in web.xml for setting UTF-8 to request and response as below
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest request = (HttpServletRequest) servletRequest;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
and i defined facelet page in this style, but problem not resolved
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:metadata>
<f:viewParam name="q" value="#{searchBean.query}"/>
</f:metadata>
.....
</html>
I test this with Myfaces 2.0.5 and Mojarra 2.0.5
Apparently you're using a server configuration which interprets GET query strings using a different character encoding by default. For example Tomcat interprets them as ISO-8859-1 by default. You need to open Tomcat's /conf/server.xml and add URIEncoding attribute to the <Connector> element with a value of UTF-8.
<Connector ... URIEncoding="UTF-8">
That filter is by the way totally unnecessary. Remove it. JSF 2.x on Facelets defaults to UTF-8 at all levels already. Besides, the HttpServletRequest#setCharacterEncoding() doesn't affect GET requests, it affects POST requests only.
See also:
Tomcat HTTP connector configuration reference
Unicode - How to get the characters right?
I'm trying with Javaee6 with GlassFish3.1.
I created a web project (with JSF2.0), with only one file, one.xhtml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1" />
<title>ONE</title>
</h:head>
<h:body>
<h:form id="oneForm">
<h:commandButton id="oneButton" value="To Two" action="two" />
</h:form>
</h:body>
</html>
I've not added web.xml, as it is optional for javaee6.
I added faces-config.xml, so that default servlet mapping works. (with only top element)
<faces-config version="2.0" .../>
When I hit command-button on the page, I'm expecting 'Page Not Found' error. But the page just reloads. If I actually put file two.xhtml, it works correctly, but in it's absence, does not give error, which I'm expecting.
Am I missing something, some config?
Thanks in advance.
This is expected. JSF works with so-called navigation cases. If it doesn't find a matching case, it will just postback to the same view. This is specified in the JSF 2.0 specification (pick the one for evaluation).
7.4.2 Default NavigationHandler Algorithm
...
The default NavigationHandler implementation must behave as if it were performing the following algorithm (although optimized implementation techniques may be utilized):
If no navigation case is matched by a call to the handleNavigation() method, this is an indication that the current view should be redisplayed. As of JSF 2.0, a null outcome does not unconditionally cause all navigation rules to be
skipped.
...
However, when you set the JSF project stage to Development by a context parameter in web.xml,
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
then clicking the button should then result in a development warning message in the postback,
Unable to find matching navigation case with from-view-id '/one.xhtml' for action 'two' with outcome 'two'
so that eventual developer mistakes are at least put in attention.