★★★ 邪!邪!!邪!!!急!急!!急!!! ★★★
近来学习STRUTS,现在学习dispatch
业务逻辑图(struts-config.xml design视图如下):
Choice.jsp---input---> ChoiceAction.java
|
|---choice_one-----> ChoiceOne.jsp
|---choice_one-----> ChoiceOne.jsp
|---choice_one-----> ChoiceOne.jsp
--------------Choice.jsp-------------
<%@ page language= "java " pageEncoding= "GB18030 "%>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-bean " prefix= "bean " %>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-html " prefix= "html " %>
<HTML>
<HEAD>
<TITLE> Welcome! </TITLE>
</HEAD>
<BODY>
<html:form action= "/ChoiceAction ">
<TABLE border= "0 " width= "100% ">
<TR>
<TD align= "right ">
<html:submit property= "method ">
<bean:message key= "button.pathone "/>
</html:submit>
</TD>
<TD align= "right ">
<html:submit property= "method ">
<bean:message key= "button.pathtwo "/>
</html:submit>
</TD>
<TD align= "right ">
<html:submit property= "method ">
<bean:message key= "button.paththree "/>
</html:submit>
</TD>
</TR>
</TABLE>
</html:form>
</BODY>
</HTML>
------------end--------------------
------------------------- struts-config.xml --------------------------
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN " "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd ">
<struts-config>
<data-sources />
<form-beans >
<form-bean name= "ChoiceForm " type= "org.apache.struts.action.DynaActionForm " />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute= "ChoiceForm "
input= "/jsp/Choice.jsp "
name= "ChoiceForm "
parameter= "method "
path= "/ChoiceAction "
scope= "request "
type= "com.yourcompany.struts.action.ChoiceAction ">
<forward
name= "choice_two "
path= "/jsp/pages/ChoiceTwo.jsp "
contextRelative= "true " />
<forward
name= "choice_one "
path= "/jsp/pages/ChoiceOne.jsp "
contextRelative= "true " />
<forward
name= "choice_three "
path= "/jsp/pages/ChoiceThree.jsp "
contextRelative= "true " />
</action>
</action-mappings>
<message-resources parameter= "com.yourcompany.struts.ApplicationResources " />
</struts-config>
----------------------------------end -----------------------
资源文件如下
---------ApplicationResources.properties---------
button.pathone=Path One
button.pathtwo=Path Two
button.paththree=Path Three
----------------------------------
----------------------------ChoiceAction.java------------------------
package com.yourcompany.struts.action;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;
public class ChoiceAction extends LookupDispatchAction {
public ActionForward path_one(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
return (mapping.findForward( "choice_one "));
}
public ActionForward choice_two(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
return (mapping.findForward( "choice_two "));
}
public ActionForward path_three(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
return (mapping.findForward( "choice_three "));
}
protected Map getKeyMethodMap()
{
Map map = new HashMap();
map.put( "button.pathone ", "path_one ");
map.put( "button.pathtwo ", "choice_two ");
map.put( "button.paththree ", "path_three ");
return(map);
}
}
-----------------------------------------------------
-------------------------ChoiceOne.jsp-----------------
this is choiceOne page!
-------------------------------------------------------
-------------------------ChoiceTwo.jsp-----------------
this is choiceTwo page!
-------------------------------------------------------
-------------------------ChoiceThree.jsp-----------------
this is choiceThree page!
-------------------------------------------------------
此时启动TOMCAT,运行一切正常,但略为把ApplicationResources.properties改成如下所求
button.pathone=Path One
button.pathtwo=第二选择
button.paththree=Path Three
则点Choice.jsp页面中的第一个按钮 "Path One " 则正常
则点Choice.jsp页面中的第三个按钮 "Path Three " 则正常
如果点Choice.jsp页面中的第二个按钮 "第二选择 " 则出错
错误信息如下:
HTTP Status 400 - Request[/ChoiceAction] does not contain handler parameter named method
--------------------------------------------
type Status report
message Request[/ChoiceAction] does not contain handler parameter named method
description The request sent by the client was syntactically incorrect (Request[/ChoiceAction] does not contain handler parameter named method).
--------------------------------------------
Apache Tomcat/5.0.19
但如果把ApplicationResources.properties改成如下所求
button.pathone=Path One
button.pathtwo=ADAFSDFASDFAS
button.paththree=Path Three
此时启动TOMCAT,运行后点击按钮“Path One”,“ADAFSDFASDFAS”,“Path Three”一切又正常了。为什么????
[解决办法]
在formBean中的reset()方法中加入以下代码即可,如果是繁体,则用BIG5
try {
request.setCharacterEncoding( "GB2312 ");
}
catch (UnsupportedEncodingException ex) {
System.out.println(ex.getMessage());
}
原因:表单提交的默认字符编码是iso,而页面信息包含了gb2312、gbk、big5等字符,那么可以肯定的说提交的链接就已经是乱码了,于是出现no method等错误提示,经过reset()设置requeset的字符编码后组装出来的提交链接才是正确的。
不对的话欢迎拍砖!
[解决办法]
ApplicationResources.properties
面不能放中文的.
你在TOMCAT下面把Server.xml中的server 中加上URIEncoding= "UTF-8 ".