Skip to content

Commit 9bd2477

Browse files
committed
fix(app): solve encoding problems with Tomcat
1 parent e845880 commit 9bd2477

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

src/main/java/config/AppWebAppInitializer.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import config.root.RootConfig;
44
import config.web.WebConfig;
55
import javax.servlet.MultipartConfigElement;
6-
import javax.servlet.Filter;
6+
import javax.servlet.FilterRegistration;
77
import org.springframework.web.filter.CharacterEncodingFilter;
88
import javax.servlet.ServletContext;
99
import javax.servlet.ServletException;
@@ -38,13 +38,16 @@ protected void customizeRegistration(ServletRegistration.Dynamic registration) {
3838
@Override
3939
public void onStartup(ServletContext servletContext) throws ServletException {
4040
super.onStartup(servletContext);
41+
// set active profiles
4142
servletContext.setInitParameter("spring.profiles.active", "development");
42-
}
43-
44-
@Override
45-
protected Filter[] getServletFilters() {
46-
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
47-
characterEncodingFilter.setEncoding("UTF-8");
48-
return new Filter[]{characterEncodingFilter};
43+
// add CharacterEncodingFilter solve encode problems with Tomcat server
44+
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding-filter", new CharacterEncodingFilter());
45+
encodingFilter.setInitParameter("encoding", "UTF-8");
46+
encodingFilter.setInitParameter("forceEncoding", "true");
47+
/*
48+
To make sure the characterEncodingFilter is first in the chain you need to change the middle argument in addMappingForUrlPatterns to false.
49+
The value false ensures that the CharacterEncodingFilter is the first filter in the chain, the value true adds the filter to the end of the filterChain
50+
*/
51+
encodingFilter.addMappingForUrlPatterns(null, false, "/*");
4952
}
5053
}

src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
app.active.profiles=development
12
hibernate.dialect=org.hibernate.dialect.MySQLDialect
23
hibernate.show_sql=true
34
hibernate.hbm2ddl.auto=update

src/main/webapp/WEB-INF/templates/admin/fragment/post.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta charset="UTF-8">
77
</head>
88
<body>
9-
<form th:fragment="form(target, delete)" enctype="multipart/form-data" id="postForm" name="postForm" method="post" action="#" th:action="${target}" th:object="${post}">
9+
<form th:fragment="form(target, delete)" accept-charset="UTF-8" enctype="multipart/form-data" id="postForm" name="postForm" method="post" action="#" th:action="${target}" th:object="${post}">
1010
<th:block th:replace="admin/fragment/alerts::form-errors"></th:block>
1111
<div class="row control-group">
1212
<div class="form-group col-xs-12 floating-label-form-group controls">

src/main/webapp/WEB-INF/templates/admin/user/self.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<div class="panel-title text-capitalize" th:text="#{users.profile.title}">Create new Post</div>
1919
</div>
2020
<div class="panel-body">
21-
<form id="userProfile" name="userProfile" method="post" action="#" th:action="${target}" th:object="${user}">
21+
<form id="userProfile" accept-charset="UTF-8" name="userProfile" method="post" action="#" th:action="${target}" th:object="${user}">
2222
<th:block th:replace="admin/fragment/alerts::form-errors"></th:block>
2323
<div class="row control-group">
2424
<div class="form-group col-xs-12 floating-label-form-group controls">

0 commit comments

Comments
 (0)