Struts 常见问题与解决方法1

来源:互联网 发布:在淘宝怎样搜到冰毒 编辑:程序博客网 时间:2024/04/28 23:22
 
如何支持中文
关于Struts支持中文的问题,大多都是写个过滤器在doFilter函数里设置字符集
    package com.web;
import javax.servlet.*;
import java.io.IOException;
public class EncodeFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
    this.encoding = null;
    this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
                      FilterChain chain)
throws IOException, ServletException {
   if (ignore || (request.getCharacterEncoding() == null)) {
     String encoding = selectEncoding(request);
     if (encoding != null)
       request.setCharacterEncoding(encoding);
   }
   chain.doFilter(request, response);
}
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"))
      this.ignore = true;
    else if (value.equalsIgnoreCase("yes"))
      this.ignore = true;
    else
      this.ignore = false;
}
}
然后,在web.xml文件配置一个过滤器,给出encoding和ignore设置过滤*.do就可以了,如下:
<filter>
           <filter-name>Set Character Encoding</filter-name>
           <filter-class>com.web. EncodeFilter </filter-class>
           <init-param>
                  <param-name>encoding</param-name>
                  <param-value>GB2312</param-value>
           </init-param>
           <init-param>
                  <param-name>ignore</param-name>
                  <param-value>true</param-value>
           </init-param>
    </filter>
    <!-- Filter Mapping -->
    <filter-mapping>
           <filter-name>Set Character Encoding</filter-name>
           <url-pattern>*.do</url-pattern>
    </filter-mapping>
 
 
如何扩展validation.xml验证文件,写自己的验证函数
Struts本身已经为我们提供了很多验证规则,这些规则即可以在客户端通过javascript验证(可以参考validation-rules.xml文件),也可以在服务器端验证(参考struts源代码中org.apache.struts.validator. FieldChecks类)。但有些时候,比较特殊的验证我们就需要自己写验证规则了,比如验证两个字段值是否相等,在会员注册中经常用的就是输入密码和确认密码一致。在Struts自带的文档中就有这个例子,请参考:
http://localhost:8080/struts-documentation/userGuide/dev_validator.html
写一个自己验证类
 public class CustomValidator {
      public CustomValidator() {
            super();
       }
       public static boolean validateTwoFields(Object bean,ValidatorAction va,Field field,
                                ActionErrors errors,
                               HttpServletRequest request) {
String value =ValidatorUtil.getValueAsString(bean, field.getProperty());
          String sProperty2 = field.getVarValue("secondProperty");
          String value2 = ValidatorUtil.getValueAsString(bean, sProperty2);
          if (!GenericValidator.isBlankOrNull(value)) {
              try {
                  if (!value.equals(value2)) {
                      errors.add(field.getKey(),
Resources.getActionError(request, va, field));
 return false;
                  }
              } catch (Exception e) {
                  errors.add(field.getKey(),
                      Resources.getActionError(request, va, field));
                  return false;
              }
          }
          return true;
      }
}
然后在validation.xml文件里声明如下
   <global>
              <!-- Custom Validator -->
              <validator name="twofields"
                   classname="CustomValidator"
                   method="validateTwoFields"
                   methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionErrors,
                       javax.servlet.http.HttpServletRequest"
                   msg="errors.twofields" />
    </global>
剩下的就是像其他规则一样在Validation.xml文件里添加你想验证FormBean的属性了
 
 
 
在写完jsp页面后,测试运行时,找不到Form bean 或页面不能显示
在struts 中,当你想测试你的页面能否正常显示时,要确保你的formBean和对应的Action正确,否则,页面会不能正常显示,出现异常。所以最好先写From和Action,(当然Action可以只是一个空的)
 
 
对一些类似的操作想放在一起,不想每个操作都写一个Action
    对于一些类似的操作,可以将每个Action操作写在一个DispatchAction中,定义不同的操作,配置Struts_config.xml文件中的<action></action> 加入parameter属性,如下:
            <action parameter="method" type="com.web.action.BaoYangListAction" scope="request" path="/qcby/baoYangListAction">
      <forward name="baoYangList" path="/qcby/qcby_list.jsp" />
    </action>
      在页面调用时为"/qcby/baoYangListAction.do?method=****"   其中****为action定义的函数名称,这样的好处就是避免了每个操作都要写一个Action;
 
strturs_config.xml文件中的<action> scope属性的值
      在配置strturs_config.xml文件中的<action>时,要注意scope的值,默认的是"session",所以一般我们会将scope设置为"request"
 
 
<bean:write>标签中Filter属性问题(想过滤一些其他struts没提供的特殊字符)
在使用<bean:write>的标签时,默认的fitler属性是true,(用于过滤html的<>,空格等),对于一些其他的特殊字符转换,比如回车<br>,我们可以自己写一个过滤函数将回车转换包含进去,通过自己的程序过滤后,传给jsp页面显示,这时要记得
 设置<bean:write fitler="false">,参考struts源代码中的
   org.apache.struts.util. ResponseUtils类的filter()函数,这就是Struts中用在<bean:write>标签的过滤函数
 
 
如何转换ApplicationResources.properties文件中中文提示消息问题
      可以先写在ApplicationResources.properties文件里采用中文书写信息,然后再用jdk/bin目录下的native2ascii工具,来转换字符,如下:
   native2ascii -encoding gb2312 ApplicationResources.properties
ApplicationResources_zh.properties
 
 
saveErrors(HttpServletRequest request,ActionErrors errors)函数使用问题
在action 中如果有错误消息要返回到页面,一般采用
saveErrors(HttpServletRequest request,ActionErrors errors)函数,在页面中调用<html:errors/>就可以显示到页面,但我在使用以上函数时会出现异常(主要是一些过滤方面的异常),所以采用其原代码那样用request.setAttribute(Globals.ERROR_KEY, errors)就可以
 
Struts 分页问题
关于Struts的分页,目前解决方法主要就是通过<logic:iterate>来实现,基本思想就是
写一个分页器(一个Bean)主要用来记录当前的页面,记录数,和页面的大小等属性,
jsp页面传给Action 查询条件,主要包在QueryFormBean中,另外还要有第几页。在Action中创建一个指定页面大小的分页器,然后从后台取数据,并通过计算填充分页器的其他属性(如总记录数,当前页面等)同时截取要显示的记录放到一个Collection中,将分页器和Collection通过request.setAttribute()返回给要显示的页面,然后用<logic:iterate>和<bean:write>将数据显示出来就可以了。
原创粉丝点击