Spring form and controller UTF-8 bad encoding - post

I have a problem with my UTF-8 encoding. My webapp uses french words that are correctly displayed in my jsp, but not in my controller after a POST.
For example, in my jsp I have:
Prénom de mon père
and when I post the form, the controller gets:
Prénom de mon père
The characterEncodingFilter is the first filter in the file as described in this post
Here is my jsp:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!doctype html>
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="content-language" content="fr">
...
</head>
<form:form class="form-horizontal" modelAttribute="AlimentationForm"
action="${actionUrl}" method="POST">
...
<form:input path="questions" class='input-xlarge' type='text' value='Prénom de mon père'/>
</form>
My web.xml:
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/web/*</url-pattern>
</filter-mapping>
And my application-config.xml
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename">
<beans:value>classpath:messages</beans:value>
</beans:property>
<beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>
I don't know what's wrong in my app or my configuration, have you got any idea?
Thank in advance.
EDIT: I'm using HDIV framework

This problem actually came from HDIV framework version 2.1.2. A patch is available here if you can't use the next release.
Hope it will help someone.

Make sure the JSP page encodes the data sent to the server properly. Try adding
<%#page pageEncoding="UTF-8" %>
to the top of your JSP as this ensures any data sent along http will get encoding correctly.
I believe the default http encoding is ISO-8859-1.

Related

Integrating Struts2 Sitemesh DWR

I'm attempting to integrate Struts2 ver 2.5.2, DWR ver 2.0,5, and Sitemesh ver 2.5. My DWR is (almost) already work fine. I can open [CONTEXT]/dwr/engine.js and util.js, and also [CONTEXT]/dwr/interface/Arithmetic.js.
The problem is, when I open index.jsp which contains dwr call, the dwr js variable is not defined as well the Arithmetic variable.
The index.jsp itself already decorated from the Sitemesh filter.
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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value> true </param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- Filters -->
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>jspSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<listener>
<listener- class>org.apache.struts2.dispatcher.listener.StrutsListener</listener-class>
</listener>
</web-app>
decorators.xml
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<excludes>
<pattern>/dwr/*</pattern>
</excludes>
<decorator name="basic-theme" page="basic-theme.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
dwr.xml
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://directwebremoting.org/schema/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="HelloAjaxService">
<param name="class" value="com.test.struts.ajax.HelloAjaxService" />
</create>
</allow>
</dwr>
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
<script type="text/javascript" src="/strutsSitemesh/dwr/engine.js"></script>
<script type='text/javascript' src='/strutsSitemesh/dwr/util.js'></script>
<script type='text/javascript' src='/strutsSitemesh/dwr/interface /HelloAjaxService.js'></script>
</head>
<body>
<div
style="background-color: #ffeaa7; font-weight: bold; width: 300px;"
id="divResponse">
<s:form action="hello" method="post">
<s:textfield id="name" name="name" label="Name" size="20"
onkeypress="sayHello()" />
<s:textfield name="age" label="Age" size="20" />
<s:submit name="submit" label="Submit" align="center" />
</s:form>
<script type="text/javascript">
function sayHello() {
console.log(dwr.util.getValue("name"));
HelloAjaxService.sayHelloAjax(function(data) {
dwr.util.setValue("divResponse", data);
});
}
</script>
</body>
</html>
If I commented filter-mapping sitemesh ---- the DWR call will work just fine. So I assume, the request will filtered by SitemeshFilter, and strangely it will not go to DWR filter. I tried to search about how to arrange filter in sequence, but nothing comes up. Any help appreciated. Big thanks.
So, I solved it by moving dwr js declaration to the body tag of index.jsp. It happened because in my decorators.jsp, I only put <decorator:body>, and because of that, the <head> tag in decorators.jsp take precedence of the actual .jsp file(In this case, index.jsp).

JSF error web page not found

Hello Every body i'm a beginner in j2ee developping .. i developed a jsf application , created some jsf views within , but when i try to execute it on my tomcat 7 server , all i see is a "page web introuvable" message
here is my code:
login.xhtml:
<!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>
<link rel="stylesheet" type="text/css" href="../css/styles.css"/>
<title>Authentification</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="Login:" />
<h:inputText value="#{loginBean.login}" size="30"/>
<h:outputLabel value="Mot de passe:" />
<h:inputSecret id="pwdId" value="#{loginBean.password}" size="30" />
<h:commandButton value="Valider" type="submit" action="#{loginBean.authentification}" />
<h:commandButton type="reset" value="Reset" />
</h:panelGrid>
</h:form>
<h:messages style="COLOR: #ff0000;"/>
</h:body>
</html>
index.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<% response.sendRedirect("pages/login.jsf"); %>
</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" 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>GestionBibliotheque</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>*.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>
</web-app>
JSP is not supported in JSF 2.
Create index.html with redirect to login page using meta tag:
<meta http-equiv="refresh" content="0; url=./login.jsf" />
Simple example here

s:url not replaced on decorator sitemesh

s:url tags are not being replaced to html code when using a decorator with sitemesh. In the next example will be correctly explained.
web.xml:
[...]
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/security-context.xml
WEB-INF/applicationContext.xml
</param-value>
</context-param>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
[...]
</web-app>
Login.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-15" pageEncoding="ISO-8859-15"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
**<link rel="stylesheet" type="text/css" href="<s:url value="/style/global.css" />" />**
[...]
Layout.jsp: (my sitemesh decorator)
<%# page language="java" contentType="text/html; charset=ISO-8859-15" pageEncoding="ISO-8859-15"%>
<%# taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%# taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page" %>
<%#taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%# taglib prefix="s" uri="/struts-tags"%>
<!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-15">
**<link rel="stylesheet" type="text/css" href="<s:url value="/style/global.css" />" />**
</head>
[...]
The result HTML:
<?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">
<head>
<title>Café Mirabeau</title>
**<link rel="stylesheet" type="text/css" href="<s:url value='/style/global.css' />"** />
</head>
<body>
<h1>Header</h1>
<p><b>Navigation</b></p>
<hr />
**<link rel="stylesheet" type="text/css" href="/(myapp)/style/global.css" />**
As you can see, the same line is on the login.jsp and on layout.jsp, but only on login.jsp is correctly replaced.
Anyone knows what can be happening?
Try putting your struts2 filter:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
before the sitemesh filter
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
in your web.xml.

Struts2 and Jersey in single web application produces http 500 error

It shows http 500 with jasper error, while launching the application, Im using jersey and struts2 in same project. Any particular configuration is required. Eventhough, all jar files are included, it shows error.
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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>RestExample</display-name>
<welcome-file-list>
<welcome-file>/jsp/login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.src.matchmaker.services</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.excludePattern" value="/rest/.*" />
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
</package>
</struts>
login.jsp
<%--
Document : register
Created on : Jan 31, 2013, 11:18:43 AM
Author : wifin
![enter image description here][1]--%>
<%#page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/match_login_style.css" type="text/css">
<link rel="icon" type="image/jpg" href="<%=request.getContextPath()%>/images/logo.jpg"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.7.2.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.validation.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/login_validation.js"></script>
</head>
<body>
<div class="wrap">
<div class="back" align="center">
<form id="login" name="login" action="login.action" method="post">
<div align="center" class="header">Login</div>
<div id="err"><s:actionerror/></div>
<div class="tit">
Email Id: <input type="text" name="emailId" id="emailId"/>
<div class="clr"></div>
</div>
<div class="tit">
Password: <input type="password" name="password" id="password"/>
<div class="clr"></div>
</div>
<div>
<input class="btn" type="submit" name="reg" value="Login" id="reg" />
<input class="btn" type="reset" value="Cancel" id="cancel" />
</div>
</form>
<div><a class="link" href="<%=request.getContextPath()%>/jsp/register.jsp">New User?</a></div>
</div>
</div>
</body>
</html>
You page is being displayed without going through Struts2 filter. You need to call Struts2 action which will display your page as a result.
Also the org.apache.struts2.dispatcher.FilterDispatcher is deprecated since Struts 2.1.3, so if you are using version greater then 2.1.3 change FilterDispatcher to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.
Also why not to intercept all url patterns with this filter? Change this filter <url-pattern> to <url-pattern>/*</url-pattern>.
BTW using scriptlets in JSP is a bad practice, use Struts2 tags instead.

Not getting the style on main page when I try to log-out from another page?

I've developed a simple struts2 web application.
In that there are two pages index.jsp and page1.jsp which are within a folder named as Page
I used the following jar files
antlr.jar
commons-beanutils.jar
commons-digester.jar
commons-fileupload.jar
commons-logging.jar
commons-logging-1.0.4.jar
commons-validator.jar
freemarker-2.3.8.jar
jakarta-oro.jar
jsf-api.jar
jsf-impl.jar
jstl-1.2.jar
ognl-2.6.11.jar
struts.jar
struts2-core-2.0.11.2.jar
xwork-2.0.5.jar
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="helloworld" extends="struts-default">
<action name="log1" class="com.loginAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2_Sample</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
<body>
<s:a href="Page/page1.jsp">LOGIN</s:a>
</body>
</html>
Page/page1.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
<body>
<s:form action="log1" method="post">
<s:textfield name="name" label="USERNAME"></s:textfield>
<s:password name="pass" label="PASSWORD"></s:password>
<s:submit method="execute" label="LOGIN"></s:submit>
</s:form>
</body>
</html>
style.css
body
{
background:olive;
}
loginAction.java
package com;
import com.opensymphony.xwork2.ActionSupport;
public class loginAction extends ActionSupport {
public String execute() {
return SUCCESS;
}
}
The following picture shows the directory structure of the my application.
http://www.sendspace.com/file/zm46c1
when I run the application, it will direct to index.jsp page with a LOGIN hyperlink appears in that. This page has an olive background color which I defined within a style.css. Now when I click that it will then direct to page1.jsp showing two text-fields and a submit button.
THE PROBLEM:
When I click the submit button it will direct to the previous index.jsp but without any background color. Even when I click that, it shows an error page.
The following pictures attachment show all my web pages of my application.
http://www.sendspace.com/file/djnqvo
when I click the LOGIN hyperlink I'd get the error page.
Can anyone tell me why I'm getting like this.
Is there any solution for this problem.
Put <s:url value="/css/style.css"/> instead of css/style.css. It works firstly with the index.jsp, because the jsp is searching the css folder in the same directory, but when you access to the other two pages, is searching the css folder in the Page folder.

Resources