Error "The absolute uri: http://struts.apache.org/tags-html cannot be resolved in either web.xml or the jar files deployed with this application " - web.xml

i am new to Struts.i have tried an sample struts1.x application.when i run it ,it gives an error
The absolute uri: http://struts.apache.org/tags-html cannot be resolved in either web.xml or the jar files deployed with this application" my jsp page has
'<%#taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%#taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>'
web page:
<html>
<body>
<center>
<html:errors/>
<html:form action="login">
<bean:message key="label.uname"/>
:
<html:text property="uname"/>
<br>
<bean:message key="label.pwd"/>
:
<html:text property="pwd"/>
<br>
<html:submit value="LOGIN"/>
</html:form>
</center>
</body>
</html>
web.xml:
<web-app>
<servlet>
<servlet-name>Action</servlet-name>
<servlet-class>org.apache.struts.Action.ActionServlet</servlet-class>
<init-param>
<param-name>Config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

Try this
Include the below jar as dependency [in WEB-INF/lib]
struts-taglib.jar [any version, eg: struts-taglib-1.3.10.jar]
In JSP page :
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
How this works: The tag library descriptor [tld] information are defined inside the “struts-taglib.jar\META-INF\tld”
You can have a look at the answer mentioned in this SO post. I have a running example there
No getter method for property... error
So either you can have the jar file as dependency or add the tag library descriptors inside your WEB-INF, and configure in web.xml
web.xml
<taglib>
<taglib-uri>
http://struts.apache.org/tags-bean
</taglib-uri>
<taglib-location>
/WEB-INF/struts-bean.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>
http://struts.apache.org/tags-html
</taglib-uri>
<taglib-location>
/WEB-INF/struts-html.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>
http://struts.apache.org/tags-logic
</taglib-uri>
<taglib-location>
/WEB-INF/struts-logic.tld
</taglib-location>
</taglib>
There is a mapping between what you configure here and the taglib you define in jsp page.
The #taglib uri in the jsp page have to match with <taglib-uri> defined in web.xml
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

Simply you have to check Add Struts TLDs option while creating project.
As shown below in a screen-shot.
Add Struts TLDs: Lets you generate tag library descriptors for the Struts tag libraries. A tag library descriptor is an XML document which contains additional information about the entire tag library as well as each individual tag. In general this is not necessary, because you can refer to on-line URIs rather than local TLD files.

Related

Unable to Setup Spring + Maven Project

I am new to working with Spring and decided to follow this tutorial:
http://www.programcreek.com/2014/02/spring-mvc-helloworld-using-maven-in-eclipse/
My files and file structure match the tutorial, and index.jsp is working. However, when I click to go to helloworld.jsp, I get the following 404 error:
The origin server did not find a current representation for the target
resource or is not willing to disclose that one exists.
Can anybody suggest places to dig? Is there something wrong with the tutorial that is not suited to Tomcat 8.5? Or is it more likely that there is something wrong with my setup?
EDIT:
I have the following installed:
Tomcat 8.5.14
Eclipse Neon with Spring IDE
Maven 3.5.0
If it helps, Maven has been working before I tried using it with a Web/Spring (ie mvn install downloads the correct libraries)
I have included an image
of my files and a below is the actual code of the files that I believe are relevant:
UserController.java
package com.ankurmgoyal.hellotest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";
#RequestMapping("/hello")
public ModelAndView showMessage(
#RequestParam(value = "name", required = false, defaultValue = "World") String name) {
System.out.println("in controller");
ModelAndView mv = new ModelAndView("helloworld");
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}
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" 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>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h3>
Click Here
</h3>
</center>
</body>
</html>
helloworld.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>Spring 4 MVC -HelloWorld</title>
</head>
<body>
<center>
<h2>Hello World</h2>
</center>
</body>
</html>
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.ankurmgoyal.hellotest.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
index.jsp is served by DefaultServlet. The url-pattern for dispatcher is /. / pattern overrides the defaultServlet and all the calls are routed through this servlet. Change the url pattern to some non-empty string and try it out.
Refer to this SO Question for more details.

Need help on Spring MVC hello world

I am trying to learn Spring MVC on NetBeans 8.0.2 using Hibernate and frankly I am stuck. Any help is appreciated. I am only trying to do a very simple "hello world" type site.
Someone would on the first page hit a submit button and the resulting page would have a list of values from the DB. Sounds pretty simple right?
I've included 5 very short files here that will hopefully help you to help me if you are so inclined.
"web.xml", "dispatcher-servlet.xml","index.jsp", "TeamController.java", "secondView.jsp"
The way I understand how Spring MVC should work, using my files, is as follows...
1) Running the project from NetBeans, the index.jsp file is brought up.
This happens because the Web.xml is consulted, which has the following line...
"<welcome-file>redirect.jsp</welcome-file>"
once the redirect.jsp is consulted, we see that it has the following...
"<% response.sendRedirect("index.htm"); %>"
so with that redirect we go back to the web.xml which has the following...
"<servlet-name>dispatcher</servlet-name>"
"<url-pattern>*.htm</url-pattern>"
so with that request coming in as index.htm it is to be handled by the dispatcher servlet.
In the dispatcher servlet's config file, the view resolver will add the following...
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
so index.htm gets changed to /WEN-INF/jsp/index.jsp and that page is brought up.
2) At this point the index.jsp is brought up. The only thing in this file is a form with a submit button. The intent is just to have someone press the button and info from the DB is returned on screen. I have Hibernate as part of this web project and I have created a java class "Team.java" based on a DB table "Team". It is populated with a few records.
Currently what I am seeing is that once the index.jsp comes up and I hit submit, it is giving a 404.
This is the URL shown for the form
"host:port/HelloWebFour/index.htm "
if I do a view source it shows it as the index.jsp. When I hit submit it gives a 404 with this url
"host:port/HelloWebFour/team? "
btw, "HelloWebFour" is the name of my project in NetBeans.
I am not sure what is happening, if my understanding is right or if I need to add anything. any help is appreciated...
The very short code files are below...
"Web.xml"
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
"dispatcher-servlet.xml"
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="testnew1" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
"index.jsp"
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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=UTF-8">
<title>index.jsp - the submit page</title>
</head>
<body>
<h2>Hit submit to get DB information</h2>
<form:form method="GET" action="/HelloWebFour/team">
<table>
<tr>
<td>
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
"TeamController.java"
package testnew1;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TeamController {
#RequestMapping(value = "/team", method = RequestMethod.GET)
public String getTeam(#ModelAttribute("SpringWeb") Team team,
ModelMap model) {
model.addAttribute("city", team.getCity());
model.addAttribute("state", team.getState());
model.addAttribute("nickname", team.getNickname());
return "secondView";
}
}
"secondView.jsp"
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>secondView.jsp - the results page</title>
</head>
<body>
<h2>Database Information</h2>
<table>
<tr>
<td>City</td>
<td>${city}</td>
</tr>
<tr>
<td>State</td>
<td>${state}</td>
</tr>
<tr>
<td>Nickname</td>
<td>${nickname}</td>
</tr>
</table>
</body>
</html>
As far I see you Submiting to "/HelloWebFour/team but your only request handler method is mapped to /team, try to submit to just /team. I don't use any prefix on Dispatcher mapping so can be a missed .htm on url combining with the unknown request url /HelloWebFour/team.
Then if one of above solutions work, your method may fail because you are expecting a modelAttribute named as "SpringWeb" of type Team, but your form actually dont post any data and it's modelAttribute is mapped to "command", the default value, so removing this can possibly remove a further error if occurs.

JSF 2 value not getting parsed in XHTML

I am new to JSF 2 and trying to build simple hello world app.Created maven project in Eclipse Luna with maven-archetype-webapp.
Following is my files :
web.xml :
<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>*.xhtml</url-pattern>
</servlet-mapping>
HelloWorld.java :
#ManagedBean(name = "helloWorld", eager = true)
public class HelloWorld {
public HelloWorld() {
System.out.println("HelloWorld started!");
} public String getMessage() {
return "Hello World!";
}
home.xhtml :
<body>
<h1>HI....!!!</h1>
#{helloWorld.message}
<h:outputLabel value="#{helloWorld.message}" />
<h1>END....!!!</h1>
</body>
</html>
When i run, my home.xhtml prints HI....!!! and END...!!!,but nothing else.
I have searched in SO and googled. I found solutions but unfortunately, i am unable to solve it.
Some explanations on your current code:
#ManagedBean defines a managed bean to be used in Facelets by JSF. The attribute eager only applies for #ApplicationScoped beans, and your managed bean doesn't have such scope.
In JSF 2, if you don't define a scope for your bean, then it's #RequestScoped by default.
Looks like your html doesn't use any Facelets code. This is how your home.xhtml should be:
<!-- HTML5 header -->
<!DOCTYPE html>
<!-- define f: and h: usage in the current page -->
<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">
<!-- JSF will include some js files for ajax functionality -->
<h:head>
<!-- this is optional -->
<title>Home</title>
</h:head>
<!-- use <h:body> for the content of your page -->
<h:body>
<h1>HI....!!!</h1>
#{helloWorld.message}
<!--
<h:outputLabel> will render a <label> and
this component should not be used to print
direct text on html but to label other HTML components
instead, use <h:outputText>
-->
<h:outputText value="#{helloWorld.message}" />
<h1>END....!!!</h1>
</h:body>
</html>

HTTP Status 404 Spring MVC Passing Parameters

I want to start work with Spring MVC, but can not adjust a simple example. I want to just pass one parameter form one jsp to another, but have error:
HTTP Status 404 - /controller/results.jsp type Status report message
/controller/results.jsp description The requested resource
(/controller/results.jsp) is not available. Apache Tomcat/7.0.12
My web.xml code:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
Servlet-context.xml
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="edu.demidov.controller" />
</beans:beans>
Controller.java
#Controller
public class HomeController extends HttpServlet{
private static final long serialVersionUID = 4825408935018763217L;
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
EducationDaoInterface educationDaoIntfc;
#RequestMapping(value="/home", method=RequestMethod.GET)
public ModelAndView firstActionPage() {
return new ModelAndView("home");
}
#RequestMapping(value = "/result.jsp", method=RequestMethod.GET)
public String SecondActionPage(#RequestParam String firstname, Model model) throws IOException {
model.addAttribute("myname", firstname);
return "result";
}
Result.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello form result.jsp
First name: ${myname}
</body>
</html>
Home.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="myform" action="<c:url value="/results.jsp"/>" method="GET">
<input type="text" name="firstname"/>
<input type="submit"/>
</form>
</body>
</html>
Thanks.
You have wrong URL mapping.
There are two solutions.
1.
You can change the url mapping of SecondActionPage function in your controller to following.
#RequestMapping(value = "/result", method=RequestMethod.GET)
And also change the form action in Home.jsp to <c:url value="/results"/>
2.
You can change the url-pattern of your appServlet to /*. That is the standard way to url-pattern in Spring web application.
And then you need to change the request mapping and action in home.jsp as I have suggested in solution 1.
I recommend you solution 2. That is best practice.
Hope this helps you. :)
try this
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
#RequestMapping(value="/home", method=RequestMethod.GET)
public ModelAndView firstActionPage() {
return new ModelAndView("home");
}
#RequestMapping(value = "/result", method=RequestMethod.GET)
public String SecondActionPage(#RequestParam String firstname, Model model) throws IOException {
model.addAttribute("myname", firstname);
return "result";
}
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="myform" action="/result" method="GET">
<input type="text" name="firstname"/>
<input type="submit"/>
</form>
</body>
</html>
You dont need to provide complete name of jsp file with extension as it will handled by org.springframework.web.servlet.view.InternalResourceViewResolver.
Change your form action to :
<form name="myform" action="result" method="GET"> //check spelling and remove '/' from url
And controller mapping to :
#RequestMapping(value = "/result", method=RequestMethod.GET)
Hope this may help you.
I have got this after all of the tying when I click on submit button from my home jsp to another controller url is:
`http://localhost:8080/controller/result?firstname=Vadim
it seems like I passing everything corectly to my controller but why my result.jsp page doesn't not appears
HTTP Status 404 - /controller/result
type Status report
message /controller/result
description The requested resource (/controller/result) is not
available.
Apache Tomcat/7.0.12

jsp page encoding problem

I home some problems with encoding of data in JSP page (I'm using a Spring-MVC).
It looks like this:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%
response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
response.setHeader("Pragma", "no-cache"); //HTTP 1.0
response.setDateHeader("Expires", 0);
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
.....
<form method="post">
<input type="text" name="txt" value="${Name}" />
<input type="submit" value= OK />
</form>
........
My app takes a text from input text box and write it to the database (HSQLDB). When I'm using english letters to write data, it all works fine! But when I'm trying to use russian letters in input text box, it write some strange letters in incorrect encoding form. How to avoid this problem?
Any ideas?
You need to configure character encoding filter in web.xml:
<filter>
<filter-name>charsetEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>charsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Servlet container treats POST request data as ISO-8859-1 if request encoding is not specifed, whereas browser submits the form in the page encoding (UTF-8 in your case) and unsually doesn't specify the encoding, therefore you need a filter to specify the proper request encoding.
EDIT: As correctly mentioned by BalusC, it doesn't cover encoding problems with GET requests. To fix that on Tomcat you need to set URIEncoding="UTF-8" on <Connector> in conf/server.xml.
I solved this problem by adding a JSP page directive to set the encoding for each individual page:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

Resources