WebWork在取得Parameters後,從Map裡面把String取出時會出現ClassCastException
JDK:j2sdk1.4.2_05
Tomcat: 4.1.27
以下是 一個 Action程式碼 execute()部分
public String execute()
{
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
String id = (String)session.get("user_id");
OvrTimeModel ovrt = new OvrTimeModel();
years = ovrt.getYear();
months = ovrt.getMonthList();
Map params = ctx.getParameters();
if(params.get("year")==null)
{
thisYear = ovrt.getYYY();
thisMonth = ovrt.getMM();
}
else
{
thisYear = (String)params.get("year");//<---會丟出ClassCastException
thisMonth = (String)params.get("month");
}
return SUCCESS;
}
執行到 thisYear = (String)params.get("year"); 就會發生 CastException
exception
java.lang.ClassCastException
at com.webwork.apps.fees.OverTimeFees58.execute(OverTimeFees58.java:38)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:168)
at com.opensymphony.xwork.DefaultActionProxy.execute(DefaultActionProxy.java:115)
at com.opensymphony.webwork.dispatcher.ServletDispatcher.serviceAction(ServletDispatcher.java:229)
at com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:199)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
xwork.xml
<xwork>
<include file="webwork-default.xml"/>
<package name="feesWork" extends="webwork-default" namespace="/fees" >
<action name="initOvrTimeFees58" class="com.webwork.apps.fees.OverTimeFees58" >
<interceptor-ref name="defaultStack" />
<result name="success" type="dispatcher">
<param name="location">/xwork/fees/OverTimeFees58.jsp</param>
</result>
</action>>
</package>
</xwork>
/xwork/fees/OverTimeFees58.jsp
部分網頁
<form name="form1" method="post" action="initOvrTimeFees58.action">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="250" bgcolor="#FFFFFF">
<ww:select label="'年'" name="'year'" list="years" value="thisYear" />
<ww:select label="'月份'" name="'month'" list="months" value="thisMonth" />
<ww:submit value="'送出'"/>
</table>
</div>
<br>
</form>