struts1.2之LookupDispatchAction
2015-10-08 14:09
375 查看
LookupDispatchAction源代码:
public
ActionForward
execute(){
...
String
parameter
=
mapping.getParameter();
String
methodName
=
getMethodName(mapping,
form,
request,
response,
parameter);
return
dispatchMethod(mapping,
form,
request,
response,
methodName);
}
首先获得mapping中配置的parameter,假设是“action”
再看getMethodName(...)
{
String
keyName
=
request.getParameter(parameter);
if
(keyName
==
null
||
keyName.length()
==
0)
{
return
null;
}
String
methodName
=
getLookupMapName(request,
keyName,
mapping);
return
methodName;
}
keyName是从request获得的名字为action的按钮的value.
如果你的按钮是"增加",
那么keyName就是"增加",
再看getLookupMapName(...){
Locale
userLocale
=
this.getLocale(request);
lookupMap
=
(Map)
this.localeMap.get(userLocale);
if
(lookupMap
==
null)
{
lookupMap
=
this.initLookupMap(request,
userLocale);
this.localeMap.put(userLocale,
lookupMap);
}
...
//
Find
the
key
for
the
resource
String
key
=
(String)
lookupMap.get(keyName);
if
(key
==
null)
{
String
message
=
messages.getMessage(
"dispatch.resource",
mapping.getPath(),
keyName);
throw
new
ServletException(message);
}
//
Find
the
method
name
String
methodName
=
(String)
keyMethodMap.get(key);
if
(methodName
==
null)
{
String
message
=
messages.getMessage(
"dispatch.lookup",
mapping.getPath(),
key);
throw
new
ServletException(message);
}
return
methodName;
}
先看上部分,getLookupMapName有一个initLookupMap(...){
...
Iterator
iter
=
this.keyMethodMap.keySet().iterator();
while
(iter.hasNext())
{
String
key
=
(String)
iter.next();
String
text
=
resources.getMessage(userLocale,
key);
//
Found
key
and
haven't
added
to
Map
yet,
so
add
the
text
if
((text
!=
null)
&&
!lookupMap.containsKey(text))
{
lookupMap.put(text,
key);
}
}
}
return
lookupMap;
}
也就是说把你配置的keyMethodMap()里的key
和对应资源文件里的value掉个个,放到
lookupMap里.例如,("增加","button.add")
("更新","button.update").
好,再看
getLookupMapName下半部分
//
Find
the
key
for
the
resource
String
key
=
(String)
lookupMap.get(keyName);
if
(key
==
null)
{
String
message
=
messages.getMessage(
"dispatch.resource",
mapping.getPath(),
keyName);
throw
new
ServletException(message);
}
//
Find
the
method
name
String
methodName
=
(String)
keyMethodMap.get(key);
if
(methodName
==
null)
{
String
message
=
messages.getMessage(
"dispatch.lookup",
mapping.getPath(),
key);
throw
new
ServletException(message);
}
意思很明显了,用"增加"查找到key="button.add"
,
之后再到keyMthodMap.get(
"button.add")
从而得到你定义的方法.
所以,答案就出来了,
getMethodName(...)
{
String
keyName
=
request.getParameter(parameter);
// 这里得到的汉字不是"增加"而是乱码,原因就是因为容器默认的编码是ISO-8859-01.
//
而你使用的是Utf-8
.
...
}
解决办法:
========================================
就是要转码,加Filter也是为了转码(ISO8859,utf-8),但是也只能转post方法的编码.所以要把你的
<form
method="post">或者使用<html:form>.
public
ActionForward
execute(){
...
String
parameter
=
mapping.getParameter();
String
methodName
=
getMethodName(mapping,
form,
request,
response,
parameter);
return
dispatchMethod(mapping,
form,
request,
response,
methodName);
}
首先获得mapping中配置的parameter,假设是“action”
再看getMethodName(...)
{
String
keyName
=
request.getParameter(parameter);
if
(keyName
==
null
||
keyName.length()
==
0)
{
return
null;
}
String
methodName
=
getLookupMapName(request,
keyName,
mapping);
return
methodName;
}
keyName是从request获得的名字为action的按钮的value.
如果你的按钮是"增加",
那么keyName就是"增加",
再看getLookupMapName(...){
Locale
userLocale
=
this.getLocale(request);
lookupMap
=
(Map)
this.localeMap.get(userLocale);
if
(lookupMap
==
null)
{
lookupMap
=
this.initLookupMap(request,
userLocale);
this.localeMap.put(userLocale,
lookupMap);
}
...
//
Find
the
key
for
the
resource
String
key
=
(String)
lookupMap.get(keyName);
if
(key
==
null)
{
String
message
=
messages.getMessage(
"dispatch.resource",
mapping.getPath(),
keyName);
throw
new
ServletException(message);
}
//
Find
the
method
name
String
methodName
=
(String)
keyMethodMap.get(key);
if
(methodName
==
null)
{
String
message
=
messages.getMessage(
"dispatch.lookup",
mapping.getPath(),
key);
throw
new
ServletException(message);
}
return
methodName;
}
先看上部分,getLookupMapName有一个initLookupMap(...){
...
Iterator
iter
=
this.keyMethodMap.keySet().iterator();
while
(iter.hasNext())
{
String
key
=
(String)
iter.next();
String
text
=
resources.getMessage(userLocale,
key);
//
Found
key
and
haven't
added
to
Map
yet,
so
add
the
text
if
((text
!=
null)
&&
!lookupMap.containsKey(text))
{
lookupMap.put(text,
key);
}
}
}
return
lookupMap;
}
也就是说把你配置的keyMethodMap()里的key
和对应资源文件里的value掉个个,放到
lookupMap里.例如,("增加","button.add")
("更新","button.update").
好,再看
getLookupMapName下半部分
//
Find
the
key
for
the
resource
String
key
=
(String)
lookupMap.get(keyName);
if
(key
==
null)
{
String
message
=
messages.getMessage(
"dispatch.resource",
mapping.getPath(),
keyName);
throw
new
ServletException(message);
}
//
Find
the
method
name
String
methodName
=
(String)
keyMethodMap.get(key);
if
(methodName
==
null)
{
String
message
=
messages.getMessage(
"dispatch.lookup",
mapping.getPath(),
key);
throw
new
ServletException(message);
}
意思很明显了,用"增加"查找到key="button.add"
,
之后再到keyMthodMap.get(
"button.add")
从而得到你定义的方法.
所以,答案就出来了,
getMethodName(...)
{
String
keyName
=
request.getParameter(parameter);
// 这里得到的汉字不是"增加"而是乱码,原因就是因为容器默认的编码是ISO-8859-01.
//
而你使用的是Utf-8
.
...
}
解决办法:
========================================
就是要转码,加Filter也是为了转码(ISO8859,utf-8),但是也只能转post方法的编码.所以要把你的
<form
method="post">或者使用<html:form>.
相关文章推荐
- Spring中配置数据库连接池
- MyEclipse快捷键
- 设置Eclipse、MyEclipse默认workspace路径
- javac和java命令
- 浅谈Java中的hashcode方法
- 字符串的正则表达式
- 20个非常有用的Java程序片段
- Java 理论与实践: 变还是不变?
- 通过分析 JDK 源代码研究 Hash 存储机制
- JavaWeb的各种中文乱码终极解决方法
- Java____变量的初始化与赋值相关
- 基于Maven的Spring + Spring MVC + Mybatis的环境搭建
- Java中字符串switch的实现细节
- 通过分析 JDK 源代码研究 TreeMap 红黑树算法实现
- Java 序列化的高级认识
- java自带线程池和队列详细讲解
- JDK源码分析之String篇
- Java 线程的状态图
- 将elcipse生成的javadoc文档生成chm文件
- Java动态代理简单应用