1 借助spring

web.xml中增加

 1<filter>   
 2<filter-name>Set Character Encoding</filter-name>   
 3<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
 4<init-param>   
 5    <param-name>encoding</param-name>   
 6    <param-value>utf8</param-value>   
 7</init-param>   
 8</filter>   
 9<filter-mapping>   
10    <filter-name>Set Character Encoding</filter-name>   
11    <url-pattern>/*</url-pattern>   
12</filter-mapping>
13
14

配置文件即可,如果不用spring怎么办呢?有办法啊

 1<filter>
 2   <filter-name>encodingFilter</filter-name>
 3   <filter-class>com.rda.commons.filter.ChineseFilter</filter-class>
 4   <init-param>
 5    <param-name>encoding</param-name>
 6    <param-value>UTF-8</param-value>
 7   </init-param>
 8   <init-param>
 9    <param-name>forceEncoding</param-name>
10    <param-value>true</param-value>
11   </init-param>
12</filter>
13<filter-mapping>
14   <filter-name>encodingFilter</filter-name>
15   <url-pattern>/*</url-pattern>
16</filter-mapping>
17
18

配置文件,然后呢,转码的过滤器就要自己写咯

 1package com.filter;
 2
 3import java.io.IOException;
 4import javax.servlet.Filter;
 5import javax.servlet.FilterChain;
 6import javax.servlet.FilterConfig;
 7import javax.servlet.ServletException;
 8import javax.servlet.ServletRequest;
 9import javax.servlet.ServletResponse;
10import javax.servlet.http.HttpServlet;
11import javax.servlet.http.HttpServletRequest;
12import javax.servlet.http.HttpServletResponse;
13
14/**
15
16* 创建人:金鑫
17* 创建时间:2008-9-21 下午03:27:03
18* 类作用:中文转码过滤器
19*
20*/

21@SuppressWarnings("serial")
22public class ChineseFilter extends HttpServlet implements Filter
23{
24private FilterConfig filterConfig;
25
26public void init(FilterConfig filterConfig) throws ServletException
27{
28   this.filterConfig = filterConfig;
29}

30
31public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
32{
33   try
34   {
35    String encoding = filterConfig.getInitParameter("encoding");
36    //从WEB.xml配置文件中取出参数,这样我们可以通过配置修改编码格式.
37    request.setCharacterEncoding(encoding);//设置请求的编码格式
38
39    filterChain.doFilter(request, response);
40   }

41   catch (ServletException sx)
42   {
43    filterConfig.getServletContext().log(sx.getMessage());
44   }

45   catch (IOException iox)
46   {
47    filterConfig.getServletContext().log(iox.getMessage());
48   }

49}

50
51public void destroy()
52{
53}

54
55protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
56{
57   super.doGet(arg0, arg1);
58}

59
60protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
61{
62   super.doPost(arg0, arg1);
63}

64
65/**
66* @function 验证数据是否为空,如果为空则转换
67@param param
68@return String
69*/

70public String checkNull(String param)
71{
72   if (param == null || param.equals(""))
73   {
74    return "";
75   }

76   else
77   {
78    return param;
79   }

80}

81}

82
83

OK,完美解决掉啦。

其实解决乱码很简单的,就是通过过滤器来实现的,呵呵很简单吧?

本文来自“青岛金鑫-java技术攻略”url:http://hi.baidu.com/%C7%E0%B5%BA%BD%F0%F6%CE/blog/item/a726f523915fe4559922ed57.html
posted @ 2008-12-26 13:53 勒紧皮带向前冲 阅读(283) | 评论 (2)编辑 收藏
 
getCurrentSession()与openSession()的区别?
 * 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()
   创建的session则不会
 * 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()
   创建的session必须手动关闭
  
使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
 * 如果使用的是本地事务(jdbc事务)
 <property name="hibernate.current_session_context_class">thread</property>
 * 如果使用的是全局事务(jta事务)
 <property name="hibernate.current_session_context_class">jta</property> 
posted @ 2008-12-23 09:44 勒紧皮带向前冲 阅读(547) | 评论 (1)编辑 收藏
 
最近学习spring,看尚学堂的视屏在做AOP的一个demo调试时发现报
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userManager' defined in class path resource [applicationContext-bean.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut allAddMethod
在网上收了下,发现是spring的版本与java的版本不一致的原因。因此自己重新下了spring的包,运行通过。
posted @ 2008-12-20 13:58 勒紧皮带向前冲 阅读(308) | 评论 (0)编辑 收藏
 

1、struts国际化的配置
 * 在struts-config.xml文件中加入:<message-resources parameter="MessageResources" />
 
2、提供不同版本的国际化资源文件,中文需要采用native2ascii转换成unicode

3、在jsp中采用<bean:message>标签来读取国际化消息文本

4、了解利用struts默认将locale放到session中的特性,完成采用编程的方式切换语言设置
 * 参见:ChangeLanguageAction.java
 
5、消息文本的国际化处理,共有三个步骤:
 * 创建国际化消息
 * 传递国际化消息
 * 显示国际化消息
 
如何创建国际化消息?
 理解ActionMessage和ActionMessages两个对象的区别
 
如何传递国际化消息?
 * 调用saveMessage()传递普通消息,调用saveErrors传递错误消息
 
如何显示国际化消息?
 通过<html:messages>标签显示消息(可以显示普通消息和错误消息)
 通过<html:errors>显示消息(只能显示错误消息) 

当将国际化资源文件放入到一个包时,在struts-config.xml文件中加入:<message-resources parameter="包名.MessageResources" />

ChangeLanguageAction.java

 1import java.util.Locale;
 2
 3import javax.servlet.http.HttpServletRequest;
 4import javax.servlet.http.HttpServletResponse;
 5
 6import org.apache.struts.Globals;
 7import org.apache.struts.action.Action;
 8import org.apache.struts.action.ActionForm;
 9import org.apache.struts.action.ActionForward;
10import org.apache.struts.action.ActionMapping;
11
12public class ChangeLanguageAction extends Action {
13
14    @Override
15    public ActionForward execute(ActionMapping mapping, ActionForm form,
16            HttpServletRequest request, HttpServletResponse response)
17            throws Exception {
18        String lang = request.getParameter("lang");
19        
20        Locale currentLocale = Locale.getDefault(); 
21        if ("zh".equals(lang)) {
22            currentLocale = new Locale("zh""CN");
23        }
else if("en".equals(lang)) {
24            currentLocale = new Locale("en""US");
25        }

26        //request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
27        
28        this.setLocale(request, currentLocale);
29        return mapping.findForward("index");
30    }

31
32}



 

posted @ 2008-12-15 10:46 勒紧皮带向前冲 阅读(885) | 评论 (2)编辑 收藏
 

1、了解缺省Locale是由操作系统决定的,Locale是由语言和国家代码组成

2、国际化资源文件是由baseName+locale组成,如:MessageBundle_en_US.properties
baseName是任意合法的文件名

3、native2ascii命令的位置和用法
 * 位置:JAVA_HOME/bin
 * 使用native2ascii.exe  o.properties MessagesBundle_zh_CN.properties

例:
 1package com.bjsxt.i18n;
 2
 3import java.text.MessageFormat;
 4import java.util.Locale;
 5import java.util.ResourceBundle;
 6
 7public class I18nSample {
 8
 9    public static void main(String[] args) {
10        
11        Locale defaultLocale = Locale.getDefault();//获取默认的本地化
12        System.out.println("default country=" + defaultLocale.getCountry());
13        System.out.println("default language=" + defaultLocale.getLanguage());
14        
15        //Locale currentLocale = new Locale("en", "US");//通过初始化指定语言和国际本地化
16        //Locale currentLocale = new Locale("zh", "CN");
17        
18        Locale currentLocale = new Locale("ja""JP");
19        
20        ResourceBundle rb = ResourceBundle.getBundle("res.MessagesBundle", currentLocale);//获取本地化配置
21        //System.out.println(rb.getString("k1"));//通过配置文件中的key显示
22        //System.out.println(rb.getString("k2"));
23        
24        MessageFormat mf = new MessageFormat(rb.getString("k1"));//通过站位符显示指定的信息
25        System.out.println(mf.format(new Object[]{"Tom"}));//对占位符填充
26        //System.out.println(mf.format(new Object[]{"张三"}));
27    }

28}

29
o.properties文件
1k1=你好,{0}
2k2=再见
缺省的properties文件MessagesBundle.properties
1k1=hello,{0}
2k2=good bye
英文文件MessagesBundle_en_US.properties
1k1=hello,{0}
2k2=good bye
中文文件MessagesBundle_zh_CN.properties
1k1=\u4f60\u597d,{0}
2k2=\u518d\u89c1
3
posted @ 2008-12-15 09:44 勒紧皮带向前冲 阅读(754) | 评论 (0)编辑 收藏
 
转自 http://jinguo.javaeye.com/blog/225596
关键字: oracle的rownum原理和使用
Oracle的rownum原理和使用
在Oracle中,要按特定条件查询前N条记录,用个rownum就搞定了。
select * from emp where rownum <= 5
而且书上也告诫,不能对rownum用">",这也就意味着,如果你想用
select * from emp where rownum > 5
则是失败的。要知道为什么会失败,则需要了解rownum背后的机制:
1 Oracle executes your query.

2 Oracle fetches the first row and calls it row number 1.

3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.

4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).

5 Go to step 3.

了解了原理,就知道rownum>不会成功,因为在第三步的时候查询出的行已经被丢弃,第四步查出来的rownum仍然是1,这样永远也不会成功。

同样道理,rownum如果单独用=,也只有在rownum=1时才有用。



对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,而且rownum不能以任何表的名称作为前缀。
举例说明:
例如表:student(学生)表,表结构为:
ID       char(6)      --学号
name    VARCHAR2(10)   --姓名
create table student (ID char(6), name VARCHAR2(100));
insert into sale values('200001',‘张一’);
insert into sale values('200002',‘王二’);
insert into sale values('200003',‘李三’);
insert into sale values('200004',‘赵四’);
commit;
(1) rownum 对于等于某值的查询条件
如果希望找到学生表中第一条学生的信息,可以使用rownum=1作为条件。但是想找到学生表中第二条学生的信息,使用rownum=2结果查不到数据。因为rownum都是从1开始,但是1以上的自然数在rownum做等于判断是时认为都是false条件,所以无法查到rownum = n(n>1的自然数)。
SQL> select rownum,id,name from student where rownum=1;(可以用在限制返回记录条数的地方,保证不出错,如:隐式游标)
SQL> select rownum,id,name from student where rownum=1;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         1 200001 张一
SQL> select rownum,id,name from student where rownum =2;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
(2)rownum对于大于某值的查询条件
   如果想找到从第二行记录以后的记录,当使用rownum>2是查不出记录的,原因是由于rownum是一个总是从1开始的伪列,Oracle 认为rownum> n(n>1的自然数)这种条件依旧不成立,所以查不到记录
SQL> select rownum,id,name from student where rownum >2;
ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
那如何才能找到第二行以后的记录呀。可以使用以下的子查询方法来解决。注意子查询中的rownum必须要有别名,否则还是不会查出记录来,这是因为rownum不是某个表的列,如果不起别名的话,无法知道rownum是子查询的列还是主查询的列。
SQL>select * from(select rownum no ,id,name from student) where no>2;
        NO ID     NAME
---------- ------ ---------------------------------------------------
         3 200003 李三
         4 200004 赵四
SQL> select * from(select rownum,id,name from student)where rownum>2;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
(3)rownum对于小于某值的查询条件
如果想找到第三条记录以前的记录,当使用rownum<3是能得到两条记录的。显然rownum对于rownum<n((n>1的自然数)的条件认为是成立的,所以可以找到记录。
SQL> select rownum,id,name from student where rownum <3;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
1 200001 张一
        2 200002 王二
综上几种情况,可能有时候需要查询rownum在某区间的数据,那怎么办呀从上可以看出rownum对小于某值的查询条件是人为true的,rownum对于大于某值的查询条件直接认为是false的,但是可以间接的让它转为认为是true的。那就必须使用子查询。例如要查询rownum在第二行到第三行之间的数据,包括第二行和第三行数据,那么我们只能写以下语句,先让它返回小于等于三的记录行,然后在主查询中判断新的rownum的别名列大于等于二的记录行。但是这样的操作会在大数据集中影响速度。
SQL> select * from (select rownum no,id,name from student where rownum<=3 ) where no >=2;
        NO ID     NAME
---------- ------ ---------------------------------------------------
         2 200002 王二
         3 200003 李三
(4)rownum和排序
Oracle中的rownum的是在取数据的时候产生的序号,所以想对指定排序的数据去指定的rowmun行数据就必须注意了。
SQL> select rownum ,id,name from student order by name;
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         3 200003 李三
         2 200002 王二
         1 200001 张一
         4 200004 赵四
可以看出,rownum并不是按照name列来生成的序号。系统是按照记录插入时的顺序给记录排的号,rowid也是顺序分配的。为了解决这个问题,必须使用子查询
SQL> select rownum ,id,name from (select * from student order by name);
    ROWNUM ID     NAME
---------- ------ ---------------------------------------------------
         1 200003 李三
         2 200002 王二
         3 200001 张一
         4 200004 赵四
这样就成了按name排序,并且用rownum标出正确序号(有小到大)
posted @ 2008-12-13 11:58 勒紧皮带向前冲 阅读(210) | 评论 (0)编辑 收藏
 

今天在写hql删除语句是想通过in来实现删除多条数据,但是发现hql语句的delete使用

where中使用in会报错Operand should contain %d column(s)"。
另外在设置了二级缓存时,如果策略为read-only时,通过hql的delete通过id删除一条记录时,可以。但如果是通过session.load/get通过id先将记录查询出来,在session.delete的话,会出错。将策略改为read-write。即可。
posted @ 2008-12-12 17:31 勒紧皮带向前冲 阅读(519) | 评论 (0)编辑 收藏
 

读取 Cookie 集合,遍历集合找到所需的 Cookie ,如果找到保存进 Session ,否则跳转到登录页面;

所需资源:

ReadCookieAction.java- 读取 Cookie 集合,如果找到对应 Cookie ,写入 Session ;

Login.jsp- 登录用页面;

LoginSubmit.java- 记录登录信息,并写入 Session ;

LoginOk.jsp- 读取 Session ,并显示。

读 Cookie 的方法

Cookie[] cookies = request.getCookies();

if (cookies != null ) {

    for ( int i=0; i<cookies. length ; i++) {

       Cookie cookie = cookies[i];

       if (cookie.getName().equals( "userInfo" )) {

           String value = cookie.getValue();

           String[] info = value.split( "_" );

           UserForm userForm = new UserForm();

           userForm.setUserName(info[0]);

           userForm.setUserPassword(info[1]);

           request.getSession().setAttribute( "userForm" , userForm);

           return mapping.findForward( "ok" );

       }

    }

}

写 Cookie 的方法:

Cookie c = new Cookie( "userInfo" ,userForm.getUserName()+ "_" +userForm.getUserPassword());

c.setComment( "A test cookie" );

c.setMaxAge(120);

response.addCookie(c);

posted @ 2008-12-12 10:57 勒紧皮带向前冲 阅读(769) | 评论 (1)编辑 收藏
 
     摘要:   1<%@ page language="java" contentType="text/html; charset=GB18030"   2    pageEncoding="GB18030"%>   3<%@ taglib...  阅读全文
posted @ 2008-12-11 15:06 勒紧皮带向前冲 阅读(213) | 评论 (0)编辑 收藏
 
在采用struts框架项目中,我们通常将模块分类,放在模块的文件夹中。这样在页面中访问时需加上文件目录名路径方可。但是我们可以通过在struts-config中配置Action时加上一个虚拟的目录名,使虚拟的目录名和模块的文件目录名同名,通过访问Action时就可自动进入该目录下,这样访问时就不需再文件目录路径了。
例:一个文件管理系统中有用户管理模块,将其放入指定的模块文件夹下,如图:

在struts-config.xml文件中配置Action的path,如图:

在页面访问时,设置连接如:<a href="user/list.do" title="请点击访问用户管理系统">用户管理系统</a>
当点击后,在地址栏中显示的地址如图:

这样就进入user目录下。再在访问改目录下的文件时,就不需加什么目录名了。
posted @ 2008-12-11 11:09 勒紧皮带向前冲 阅读(393) | 评论 (0)编辑 收藏
仅列出标题
共14页: First 上一页 4 5 6 7 8 9 10 11 12 下一页 Last