I am stuck in a big problem with encoding in my website!
I use spring 3, tomcat 6, and mysql db. I want to support German and Czech along with English in my website, I created all the JSPs as UTF-8 files, and in each jsp I include the following:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
I created messages.properties (the default which is Czech), messages_de.properties, and messages_en.properties. And all of them are saved as UTF-8 files.
I added the following to web.xml:
<filter>
<filter-name>encodingFilter</filter-name>
<filterclass>
org.springframework.web.filter.CharacterEncodingFilter</filterclass>
<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>
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>en</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
<locale-encoding-mapping>
<locale>cz</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
<locale-encoding-mapping>
<locale>de</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
And add the following to my applicationContext.xml:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basenames="messages"/>
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="locale" />
</mvc:interceptors>
<!-- Declare the Resolver -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
I set the useBodyEncodingForURI attribute to true in the element of server.xml under: %CATALINA_HOME%/conf, also another time tried to add URIEncoding="UTF-8" instead.
I created all the tables and fields with charset [utf8] and collection [utf8_general_ci]
The encoding in my browser is UTF-8 (BTW, I have IE8 and Firefox 3.6.3)
When I open the MYSQL Query browser and insert manually Czech or German data, it's being inserted correctly, and displayed correctly in my app as well.
So, here's the list of problems I have:
By default the messages.properties (Czech) should load, instead the messages_en.properties loads by default.
In the web form, when I enter Czech data, then click submit, in the Controller I print out the data in the console before to save it to db, what's being printed is not correct having strange chars, and this is the exact data that saves to db.
I don't know where's the mistake! Why can't I get it working although I did what people did and worked for them! don't know..
Please help me, I am stuck in this crappy problem since days, and it drives me crazy!
Thank you in advance.
First, if your project is using Maven, make sure that the Maven Resources Plugin has UTF-8 set as its character encoding scheme, otherwise the message properties files could be written to your target with an incorrect encoding.
Second, you're using ResourceBundleMessageSource, which uses the standard java.util.ResourceBundle and java.util.Properties, which only support ISO-8859-1 encoding. You can instead use ReloadableResourceBundleMessageSource like:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
which I discovered from this Cake Solutions blog post.
If this still does not work and you're using Tomcat as your application server try to set the following option on every <Connector> element in the server.xml:
<Connector URIEncoding="UTF-8" ...>
...
</Connector>
This did the trick for me. There might be similar options for other application servers, so you might want to check the server documentation.
1: By default the messages.properties (Czech) should load, instead the messages_en.properties loads by default.
I don't do Spring, so here's a shoot in the dark: maybe because English is ordered first and/or your platform/browser uses English as default locale? Rearrange the configuration and check accept-language in the HTTP request header to exclude one and other.
2: In the web form, when I enter Czech data, then click submit, in the Controller I print out the data in the console before to save it to db, what's being printed is not correct having strange chars, and this is the exact data that saves to db.
Is the console configured to use UTF-8 to display data? It would use the platform default encoding otherwise. In for example Eclipse you can configure it by Window > Preferences > General > Workspace > Text File Encoding.
Is the data access layer configured properly to handle the data as UTF-8? MySQL JDBC driver is known to be a bit non-smart in this. It won't use the DB-specified encoding, but the client platform default encoding. To the point, you'd like to use the useUnicode=true and characterEncoding=UTF-8 properties in the JDBC connection string. E.g.
jdbc:mysql://localhost/some_db?useUnicode=yes&characterEncoding=UTF-8
Related
Is it possible to apply ServletFilters on content delivered via an alternatedocroot?
Filter mapping
<filter-mapping>
<filter-name>StaticContentAuthenticationFilter</filter-name>
<url-pattern>/img/users/*</url-pattern>
</filter-mapping>
Alternate Docroot
<glassfish-web-app>
<context-root>/xxx</context-root>
<property name="alternatedocroot_1" value="from=/img/* dir=C:/appdata" />
</glassfish-web-app>
Problem statement:
Do some authentication before serving static content (specifically images).
Since I am serving images I cannot use a database nor can I keep the images in the .war file
Sorry! The above code works! There was an error elsewhere in the application.
First, I am a complete noob when it comes to Spring. An application was left to me to work on by a colleague who is now on vacation. He told me to leave security alone, as the final approach is not decided yet, and just develop the rest of the application.
However security is enabled and prevents access to the main web page. I've checked several documents including
Disable Spring Security from spring-security.xml file
Disable Basic Authentication while using Spring Security Java configuration
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-security.html
without finding an answer that works.
I did the following:
Added index.html to <welcome-file-list> in web.xml. This directs me to the login page which was already included in the package I took over. So, I figured I could simply disable security.
In the spring security.xml added the attribute security="none". Now I no longer get the login page. I get a blank page.
In web.xml disabled
<!-- <filter> -->
<!-- <filter-name>springSecurityFilterChain</filter-name> -->
<!-- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> -->
<!-- </filter> -->
<!-- <filter-mapping> -->
<!-- <filter-name>springSecurityFilterChain</filter-name> -->
<!-- <url-pattern>/*</url-pattern> -->
<!-- </filter-mapping> -->
Still get blank page.
I see lots of other advice, but it assumes a lot more Spring knowledge than I currently have.
For example in the 4th link above I see:
If you define a #Configuration with #EnableWebSecurity anywhere in
your application it will switch off the default webapp security
settings in Spring Boot.
I don't know what they mean. I assume this means to put these annotations on some method somewhere, but I can't believe that this can go ANYWHERE in any java class in the application. Is there an example of doing this?
Can someone point me in the correct direction? Thanks.
Comment out <intercept-url pattern="" access="" /> tags in security XML file and give access to all the pages. This should work.
This seemed to work (in spring-security.xml)
<!-- <intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> -->
<intercept-url pattern="/**" access="permitAll" />
check your web.xml or your appContext.xml to find where is loaded the spring security.xml beans(it will be like <import resource=../spring security.xml>) and comment this out , also check the beans that are loaded from there before disabling it , cause it might redirecting or whatever
I am working on Spring, JasperReports. My Spring version is 3.0.5 RELEASE, iReport-4.5.0.
I am using http://chathurangat.blogspot.in/2012/02/jasperreport-with-spring-mvc-fully.html link as sample to generate the reports. For this every thing is working fine. But when i am exporting the report to html i am not able to get the images. For this I have configured imageservlet in my web.xml and IMAGES_URI, IMAGES_DIR_NAME, IS_OUTPUT_IMAGES_TO_DIR, IS_USING_IMAGES_TO_ALIGN parameters in jasper-views.xml file.
Below is the configuration of my code in jasper-views.xml, web.xml.
Using this configurations if i run the report i am getting the 500 Error as no jasperprint document found on the httpsession.
If i configure the DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE attribute then I've get the image. But i am not getting how to configure this in jasper-views.xml file. This attribute is expecting the JasperPrint object but i am not creating any JasperPrint object.
Can any one help me regarding this. I am struggling a lot for this. If you want any more information i vl give you.
This is my jasper-views.xml configuration:
<util:map id="exportParameterMap">
<entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN">
<value>false</value>
</entry>
<entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR">
<value>true</value>
</entry>
<entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI">
<value>images?image=</value>
</entry>
<entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_DIR_NAME">
<value>/home/rupa/Workspace/sample/src/main/webapp/images/rupa</value>
</entry>
</util:map>
<bean id="ipHtmlReport"
class="org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView"
p:url="classpath:reports/ggsnreport.jrxml"
p:reportDataKey="datasource"
p:exporterParameters-ref="exportParameterMap">
</bean>
This is my web.xml configuration:
<servlet>
<servlet-name>image</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>image</servlet-name>
<url-pattern>/images</url-pattern>
</servlet-mapping>
I haven't done it with a pure Spring approach. I have part of the site with Spring, but the reports are generated using traditional Servlets.
If it is helpful to you, you can see this answer I wrote for a similar question.
I'm trying to map a request to static resources in a spring environment. My app server is Jetty.
In web.xml, I'm mapping various url patterns to my spring servlet:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/otherpath/*</url-pattern>
</servlet-mapping>
[many more mappings...]
Note that "/" is not mapped to my spring servlet.
In spring-servlet.xml, I'm using the mvc:resources tag to map a url to a directory with my static content:
<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
This does not work as I expected. Instead of mapping
/static/ to /WEB-INF/static/,
it maps
/static/static/ to /WEB-INF/static
The reason is that the mapping given in "mvc:resources" seems to not be relative to / but relative to the path that maps to the spring servlet.
Is there a way to consider the full path, relative to / for the mapping, not the path relative to the servlet mapping?
The solution is to not use the mvc:resources tag, but to configure the corresponding handler with a bean and a URLHandlerMapping:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true"/>
<property name="mappings">
<props>
<prop key="/static/*">staticResources</prop>
</props>
</property>
</bean>
<bean id="staticResources" class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
<property name="locations">
<list>
<value>/WEB-INF/static/</value>
</list>
</property>
</bean>
The SimpleUrlHandlerMapping with its alwaysUseFullPath property does allow more fine-grained control over the mapping.
To answer your question with one short word. No, at least I don't think so.
The servlet looks inside it's own "space" and that is after the servlet-mapping done in web.xml. That in turn is after the mapping done i your container (like tomcat)
Would it be possible to add just one servlet to / and then add two <mvc:resource />? One with /static/** and one with /otherpath/** (or whatever you need there). If not I would go with JB Nizet's solution to add two different servlets entirely.
Or you could use <mvc:default-servlet-handler/> and <spring:url>. It worked for me.
mvc:resources doesn't seem to work when application is not started on the ROOT context.
Here's the configuration that I used (note the commented bit that was doing the resource mapping to the application started under 'localhost:8080/myapp' context, though context name shouldn't be in the spring config):
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<!--<mvc:resources location="/styles" mapping="/myapp/styles/**"/>-->
<!--<mvc:resources location="/js" mapping="/myapp/js/**"/>-->
<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource
requests to the container's default Servlet -->
<mvc:default-servlet-handler/>
The trick is to use spring:url to resolve your application context. Here is what I used for that:
<spring:url value="/styles/site.css" var="site_style"/>
<link rel="stylesheet" href="${site_style}" type="text/css" media="screen"/>
I'm basically using relative paths to my root app folder, while spring takes care of adding the /myapp in front of it.
It's still pretty strange that mvc:resources doesn't do that on it's own, but at least this works and its still pretty neat.
Is it possible to define the security:intercept-url elements and security:custom-filter elements for a single security:http in two different Spring configuration files?
This is so we can cleanly reuse the security:custom-filter definitions which will be common across many applications with intercept rules that will not.
I can't simply duplicate the <security:http> element because I get BeanDefinitionParsingException: Configuration problem: Duplicate <http> element detected. I am well well aware of how to split a normal bean file with import
As requested in comment:
Spring Security versions prior to 3.1.x do not allow multiple http element definitions.
3.1 does however.
Here is the Jira issue for the feature.
This article on 3.1 changes might also be helpful.
You can define another context file in your web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-contexts/context1.xml
/WEB-INF/spring-contexts/context2.xml
</param-value>
</context-param>
Or you can define a directory where your contexts would be and name them any way you like without having to specify each context file separately:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-contexts/*
</param-value>
</context-param>
Regarding Ayusman's answer, you actually can import your security contexts into your bean contexts:
<beans>
<import resource="classpath*:/security-context-*.xml"/>
<bean><!-- blah blah --></bean>
</beans>
use the import in application context file..
custom-filter.appcontext.xml
.
.
<import resource="interceptor-url-file.xml"/>
Note that both files need to have the proper spring xml schema details and MUST be valid XML files.
I have been working on this error for 5 hours. Really stupid problem.
This error is a parse error that when you comment some lines in applicationContext-security.xml, files are not generated correctly.
Let me explain on an example code.
<port-mappings>
<port-mapping http="7001" https="7002" />
</port-mappings>
<!-- <port-mappings>
<port-mapping http="7015" https="7515" />
</port-mappings>
-->
this lines are generated as,
<port-mappings>
<port-mapping http="7001" https="7002" />
</port-mappings>
<port-mappings>
<port-mapping http="7015" https="7515" />
</port-mappings>
-->
so that, compiler tells you "duplicate element detected". Because generated file includes duplicate elements.
I hope to help you .