struts中配置过滤文件!

 

在web.xml中加上
<filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>com.ytoa.util.SetCharacterEncodingFilter</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-name>Set Character Encoding</filter-name>
    <servlet-name>action</servlet-name>
  </filter-mapping>

过滤文件为:
package com.ytoa.util;

import javax.servlet.*;
import java.io.IOException;


public class SetCharacterEncodingFilter implements Filter {

    // ----------------------------------------------------- Instance Variables


    /**
     * 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;


    // --------------------------------------------------------- Public Methods


    /**
     * 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 {

        // Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null)
                request.setCharacterEncoding(encoding);
        }

    // Pass control on to the next filter
        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"))
            this.ignore = true;
        else if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
        else
            this.ignore = false;

    }


    // ------------------------------------------------------ Protected Methods


    /**
     * 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);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一.功能简介 1. 实现一个图书管理系统。图书信息存放到一个数据库。图书包含信息:图书号、图书名、作者、价格、备注字段。 2. 系统要实现如下的基本管理功能: (1)用户分为两类:系统管理员,一般用户。 (2)提供用户注册和用户登录验证功能;其登录用户的信息有:登录用户名,登录密码等。 (3)管理员可以实现对注册用户的管理(删除),并实现对图书的创建、查询、修改和删除等有关的操作。 (4)一般用户,只能查询图书,并进行借书、还书操作,每个用户最多借阅8本,即当目前借书已经是8本,则不能再借书了,只有还书后,才可以再借阅。 二.涉及技术 Struts2框架、Hibernate框架、MySQL数据库、C3P0数据池、Jsp、HTML、CSS、JavaScript等技术。 三.设计思路 1. 基于Struts2框架和Hibernate框架进行编程设计,连接MySQL数据库实现数据的增删查改,应用Jsp、HTML、CSS、JavaScript对访问页面进行编写和美化。 2. 分别创建book表和user表,用以存放图书信息和用户数据。其user表,设有flag以区分管理员和普通用户。 3. 分别创建Book类和User类,与数据表相对应。每本书和每个用户都有唯一的id与之对应。 4. 创建映射文件User.hbm.xml和Book.hbm.xml。 5. 创建数据库配置文件hibernate.cfg.xml。 6. 创建数据库连接工具类。 7. 设计数据库操作类:UserDao类和BookDao类。UserDao用于实现所有对user表的操作,BookDao用于实现所有对book表的操作。 8. 创建分别对应UserDao类和BookDao类的Action:UserAction和BookAction。采用基于注解的方式进行Action配置。 9. 用户账号分为管理员账号和普通用户账号,注册时加以区分,登录时即可自动判断进入对应的操作主页面。 10. 管理员可实现对用户的查询显示,模糊查询,删除,批量删除,全选和取消全选等功能;可实现对图书的查询显示,模糊查询,添加,删除,批量删除,全选和取消全选等功能。 11. 普通用户可实现借书和还书功能,借书功能通过对book表的查询,将未借出的图书按照id顺序排列显示,点击表格后方的“借阅”按钮,进行确认借阅,将book表本书的borrowperson列的值改为本用户账号。对于借阅成功的图书可以在“当前借阅”进行查看。还书功能通过在“当前借阅”点击“还书”按钮,进行确认还书,将book表本书的borrowperson列的值改为“空”,本书信息将可以在“借书”界面查看。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值