关于struts里的的中文问题

charlie0895 2004-09-08 09:58:43
我需要在页面上显示的submit按纽要是中文的,代码如下
在jsp页面中:
<html:submit property="submit" value="通过"/>
<html:submit property="submit" value="驳回"/>
在action中对此进行判断
if(request.getParameter("submit").equals("通过")){......}
if(request.getParameter("submit").equals("驳回")){......}
这样的话,程序是可以很好的运行的,但action程序中出现这样的中文肯定不妥!
请问<html:submit>的用法中怎么实现,在jsp页面上显示的是中文字,但在action中比较的时候,是去比较另一个“属性”,英文的。避免java程序中出现中文
...全文
339 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
stingJavaNet 2004-11-12
  • 打赏
  • 举报
回复
问题已经解决:
问题存在:
1、用ASCII将汉字写给一个Button:解决了Button中文显示问题,但是当提交一个Button的时候,会将Button.value(中文)写入request中,这个时候存入的是一个中文的编码,这个编码显然不是中文ASCII,所以这个值最后和LookupDispatchActin中的Map进行对比并匹配导向相应方法的时候,出错。

问题解决:
加过滤器,对request中的值进行合理的编码,成功!

yijunGuo 2004-11-12
  • 打赏
  • 举报
回复
学一下STRUTS中的国际化!
ymm 2004-11-12
  • 打赏
  • 举报
回复
加过滤器SQL SERVER好像不行吧,ORACLE没有问题,我感觉就出过来,在ACTION中用一下 public static String toChinese(String str) {
try {
if (str == null) {
return null;
}
else {
str = new String(str.getBytes("ISO8859_1"), "GBK");
return str;
}
}
catch (Exception e) {
return null;
}
}
不就可以了吗,嘻嘻
stingJavaNet 2004-11-12
  • 打赏
  • 举报
回复
过滤器代码:


public class EncodingFilter
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;
}

/**
*选择并设置这个request要使用的字符编码
*/
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;
}
}

/**
*选择要使用的字符编码
*/
protected String selectEncoding(ServletRequest request)
{
return (this.encoding);
}

}

在web.xml中相应的配置

btl19792008 2004-11-12
  • 打赏
  • 举报
回复
在struts.xml中加filter?
yingwu0579 2004-11-11
  • 打赏
  • 举报
回复
学习
stingJavaNet 2004-11-11
  • 打赏
  • 举报
回复
这个我也试了,因为你不转码的话显示乱码的。
问题大概是这样:
在使用LookupDispatchAction的情况下

--jsp页面中是这样:
<html:submit property="method">
<bean:message key="button.add">
</html:submit>
……//多个submit


--Action中有一个map
protected Map getKeyMethodMap()
{
Map map = new HashMap();
map.put("button.add", "add");
......//多个
return map;
}

--用英文,没有任何问题
--用中文的转码,连不到相应的Action方法

--猜测:struts中从页面中的submit中得到button.add的具体的值“\u589e\u52a0”与map的中取到button.add的值进行比较的,因为转码格式不同,所以找不要匹配的方法,
解决:用map中的转码方式处理中文得到统一,可是现在不知道map的转码格式。
zhonghua_399 2004-11-11
  • 打赏
  • 举报
回复
button.add=\u589e\u52a0这是button.add=增加编译后的
zhonghua_399 2004-11-11
  • 打赏
  • 举报
回复
只要用j2sdk1.4.1_01\bin\native2ascii.exe把汉字编译一下就可以了
stingJavaNet 2004-11-11
  • 打赏
  • 举报
回复
Jeffbean(Jeff) 说的存在问题,偶试了一下:
1、在ApplicationResource中加入button.add="增加"是不可以的,只能用button.add=增加;
2、用button.add=增加也存在问题,因为如果用的是LookUpDispatchAction,点击相应的Button后出现NullPointerException,但是用E文可以,比如button.add=Add;

不知道怎么解决,高手请回复。




cold_blooded 2004-10-18
  • 打赏
  • 举报
回复
Jeffbean(Jeff) 说的有理,
再来一个传统的,设一个flag hidde的,按钮按下时,script改变它的值,
到了后台判断时那个按钮按下。
nbnasom 2004-10-12
  • 打赏
  • 举报
回复
学习
Jeffbean 2004-10-12
  • 打赏
  • 举报
回复

<html:form action="/dataRecord">
<html:submit property="method">
<bean:message key="button.add">
</html:submit>
<html:submit property="method">
<bean:message key="button.view">
</html:submit>
<html:submit property="method">
<bean:message key="button.update">
</html:submit>
<html:submit property="method">
<bean:message key="button.delete">
</html:submit>
</html:form>
其中button.add,button.view等是在resource.properties文件中写的键值对的键,也就是key,对应的值可以是任何文字,包括中文。
eg
button.add="增加"
struts就是用这一招来解决国际化问题的。
至于你想通过不同的提交按钮来响应不同的操作,应该使用LookUpDispatchAction来解决,需要注意的是所有的<html:submit>标签的property属性必须相同,而且需要在struts-config.xml中配置,
eg:
<action
path="/dataRecord"
type="app.recordDispatchAction"
name="dataForm"
scope="request"
input="/data.jsp"
parameter="method"/>注意parameter与<html:submit>标签的property属性必须相同
这样LookUpDispatchAction才知道是同一提交,然后LookUpDispatchAction会根据key值来确定相应的操作。详细情况参见:Struts in action
charlie0895 2004-10-12
  • 打赏
  • 举报
回复
该问题怎么没人回答呢!版主出来帮忙呀
charlie0895 2004-09-09
  • 打赏
  • 举报
回复
up
charlie0895 2004-09-08
  • 打赏
  • 举报
回复
楼上的说具体点撒!
dvictor 2004-09-08
  • 打赏
  • 举报
回复
用<html:button property="" >
</html:button>

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧