当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:15:00  来源:本站整理

Struts+Hibernate中办理汉字编码问题[Java编程]

赞助商链接



  本文“Struts+Hibernate中办理汉字编码问题[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

1.改正web.xml文件,在<web-app>标签后增添以下内容.

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.company.demo.util.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>

2.成立类文件(SetEncodingFilter.java)

package com.company.demo.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
/**
* Set All HttpRequest Encoding
*/
public class SetEncodingFilter
implements Filter {
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(selectEncoding(request));
}
chain.doFilter(request, response);
}
/**
* Place this filter into service.
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
/**
* Returns the filterConfig.
* @return FilterConfig
*/
public FilterConfig getFilterConfig() {
return filterConfig;
}
/**
* Sets the filterConfig.
* @param filterConfig The filterConfig to set
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
}


  以上是“Struts+Hibernate中办理汉字编码问题[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 在Google App Engine中操纵Struts2框架
  • 关于Struts和Spring两种MVC框架的比较
  • <b>Struts2文件上传</b>
  • 让Struts与Hibernate顺利协同工作
  • Hibernate+Spring+Struts扩大Struts
  • J2EE底子 用Struts框架开辟MVC系统步骤
  • <b>J2EE底子:Struts框架初学者从这里入门</b>
  • struts2整合tiles及配置
  • Struts2.0深化学习 命名空间,模子驱动,表单反复提交
  • 操纵Struts 2开辟RESTful服务
  • Struts2输入校验(一) validate办法-validateXxx办法
  • Struts2输入校验(二) 考据框架
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .