spring mvc encoding messy code when get value from the post request - spring

I use spring mvc to develop a web site. I have put accept-charset="UTF-8" in the form attribute and
<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>/*</url-pattern>
</filter-mapping>
in the web.xml. But still, I get the messy code when saving an entity into the database. I have searched and found the solution is always be the characterEncodingFilter in the web.xml, which I have already done. Can somebody help understand this problem?

Related

Filter mapping in TomCat for unicode prevents localhost from getting resolved

I have a webApp that is suppose to display unicode characters, which is not working with the default tomcat settings. The unicode characters are displayed as "??????". After looking into it I found a solution where adding an encodingFilter to web.xml fixes the problem.
But it creates another problem where the default tomcat landing page "localhost:8080" and all other services like "localhost:8080/manager" fail to resolve. I get a HTTP Status 404 – Not Found. Although by webapp is working properly "localhost:8080/myWebapp/servletName", URL with my webapp is resolved properly and the unicode characters are also displayed properly.
The filter that I added to web.xml is as follows
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I am pretty new to this so there must some very basic things that I don't understand. But any pointers would help.

What is the purpose of Spring CharacterEncodingFilter

I'm working on an existing project in the configuration file I found this filter that I didn't understand the the purpose of this filter Spring CharacterEncodingFilter I read some documentation but still i didn't understand how it's work :
<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>
Any help Thank you
I think documentation is pretty clear: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/CharacterEncodingFilter.html
"Servlet Filter that allows one to specify a character encoding for requests. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form."

Is CharacterEncodingFilter in spring filter Non Ascii characters?

I am using Spring mvc 3.2 and need to filter non-ascii characters from the input.
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>/*</url-pattern>
</filter-mapping>
Using Tomcat, but it is not filtering the Non-Ascii characters. Do we need to write a filter to do the above?
CharacterEncodingFilter sets up character encoding on request and responce, it not filters any input or output, it's filter because it implements http://docs.oracle.com/javaee/5/api/javax/servlet/Filter.html interface.

Endless redirect loop after ticket validation

I'm using the Liferay Portal 6.1.1 CE GA2.
After hours of research I got the following things to work:
Import from a LDAP-Server and custom mapping of an attribute to an usergroup.
Redirect to a specific page after login (based on the usergroup).
Authentication via CAS. This means getting a ServiceTicket and logging in the corresponding user.
Now I'm trying to obtain ProxyTickets so I can proxy to other applications behind the same CAS-Server.
I'm not really getting any error, but Mozilla gives me a redirection error, e.g. the page is redirected in such a way that it can never be loaded.
I googled a lot and tried different approaches but nothing helped.
My web.xml is configured as follows (I snipped out urls. If they're important I can hand them in later):
<filter>
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>* snip */cas/login</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>* snip *</param-value>
</init-param>
</filter>
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>* snip */cas/</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>* snip *</param-value>
</init-param>
<init-param>
<param-name>proxyCallbackUrl</param-name>
<param-value>https://* snip */pgtCallback</param-value>
</init-param>
<init-param>
<param-name>proxyReceptorUrl</param-name>
<param-value>/pgtCallback</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Authentication Filter</filter-name>
<url-pattern>/c/portal/login</url-pattern>
</filter-mapping>
I tried various combinations of the filter-mappings but nothing helped.
The output of the console in Eclipse hints that multiple consecutive requests are done. Each gets me a TGT, PGTIOU and PGT but after the ST is validated a new validation request is fired. This goes until Mozilla ends the redirect loop.
I also tried specifying service instead of serverName but all remains the same.
Setting the param redirectAfterValidation to false but then I get a MalformedURLException.
Hopefully I didn't forget any information, please help me.
Thanks in advance.

how to integrate UrlRewritingFilter with Struts 2.X

1) Is there a way to integrate UrlRewritingFilter with Struts2 tiles.i think there is a problem with
listeners in web xml.
<listener>
<listener-class>listener.ApplicationListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
2) how to use struts2 advanced wildcard mapping to write better urls with actions.anybody can share a working example please?
thanks.!!
This is how you need to configure your rewrite filter
web.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
Please note the change done to S2 filter
Add a rewrite.xml file under WEB-INF with entry like
<urlrewrite>
<rule>
<from>^/some/olddir/(.*)$</from>
<to type="redirect">/very/newdir/$1</to>
</rule>
</urlrewrite>
For more details refer to there documents.
There is one more option to create clean URL using NamedVariablePatternMatcher,for details refer to this blog for example and understanding
better-urls-with-struts2

Resources