At 1:44 PM +0800 10/18/05, Tony Lu wrote:
I need to create an internationalized Web application by struts,mysql and
hibernate.
Each component Character Encoding is utf8.
It runs well when I run a pure servlet to save 'Chinese Character' to
database and load it from database.
But when I implement it with struts action, the application can not save
Chinese correctly.
I really don't know why struts action can not work well. I am sure there is
no difference between them.
By the time that your action class is executing, Struts has already
read from the request input stream (to populate the ActionForm).
In any web application, if you want to set the request character
encoding, you must arrange to do it before any code will read from
the request input stream. The best way to do this in a Servlet 2.3
or later web environment is to use a ServletFilter. In fact, most of
the worked examples of writing ServletFilters on the web are to solve
this problem.
You could also do it by extending RequestProcessor (or
TilesRequestProcessor, if you use Tiles) and overriding the
processPreprocess method. However, in Struts 1.3 the
processPreprocess method is no longer used. Using a ServletFilter
will apply to any application you write in any Struts version, or
with a non-Struts approach.
Joe
Is there anywhere to set character encoding for action? Please help!!!!!
------------------------------Pure Servlet ( It runs well)
-------------------------------------------------------------
public class UtfTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String description = request.getParameter("description");
description = (description == null?"":description.trim());
try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();
item.setDescription(description);
itemDao.save(item);
}catch(Exception e){
e.printStackTrace();
}
response.sendRedirect("test.jsp");
}
}
------------------------------Action for struts ----------------------------
public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception {
req.setCharacterEncoding("UTF-8");
String description = req.getParameter("description");
description = (description == null?"":description.trim());
try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();
item.setDescription(description);
itemDao.save(item);
// Determine which action forward should be returned
return mapping.findForward("success");
}catch(Exception e){
}
}
}
--
Joe Germuska
[EMAIL PROTECTED]
http://blog.germuska.com
"Narrow minds are weapons made for mass destruction" -The Ex
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]