我正在学习一些Web开发,过去我大多只使用Java或C ++。
我正在尝试创建一个基于网络的Yahtzee游戏并且在开始时遇到一些麻烦。
尝试使用命令按钮调用函数时,我得到一个找不到异常的方法。
我尝试用0调用此函数,同样的事情发生了:
堆栈跟踪
javax.faces.el.MethodNotFoundException: javax.el.MethodNotFoundException: /index.xhtml @14,97 action="#{dice.shouldHold(varStatus.index)}": Method shouldHold not found
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at com.sun.faces.facelets.component.UIRepeat.broadcast(UIRepeat.java:1045)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.MethodNotFoundException: /index.xhtml @14,97 action="#{dice.shouldHold(varStatus.index)}": Method shouldHold not found
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 37 more
xhtml代码:
<?xml version = '1.0' encoding = 'UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Roll" action="#{dice.roll()}" ></h:commandButton>
<br/><br/>
<ui:repeat var="dice" value="#{dice.dice}" varStatus="varStatus">
<h:commandButton value="#{dice}" action="#{dice.shouldHold(varStatus.index)}" />
</ui:repeat>
</h:form>
</h:body>
</html>
java代码: dice.java: 包组件;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
*
* @author Dave
*/
@ManagedBean
@SessionScoped
public class Dice
{
private Die dice[];
private String diceString;
public Dice()
{
dice = new Die[5];
for(int i = 0; i < dice.length; i++)
dice[i] = new Die();
diceString = "";
}
@PostConstruct
public void reset()
{
for(int i = 0; i < dice.length; i++)
dice[i].reset();
diceString = "";
}
public Die[] getDice() { return dice; }
public String getDiceString() { return diceString; }
public void setDice(Die dice[]) { this.dice = dice; }
public void setDiceString (String diceString) { this.diceString = diceString; }
public void roll()
{
reset();
for(int i = 0; i < dice.length; i++)
if(!dice[i].getHold())
dice[i].roll();
}
public void shouldHold(int index)
{
if(!dice[index].getHold())
dice[index].setHold(true);
else
dice[index].setHold(false);
}
@Override
public String toString()
{
for(int i = 0; i < dice.length; i++)
diceString = dice.toString() + " ";
return diceString;
}
}
die.java:
package components;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
@ManagedBean
@SessionScoped
public class Die
{
final private int MINIMUM = 1;
final private int MAXIMUM = 6;
private int face;
private String faceString;
private boolean hold;
public Die()
{
reset();
}
public void reset()
{
face = 0;
faceString = "";
hold = false;
}
public int getFace() { return face; }
public String getFaceString() {return faceString; }
public boolean getHold() { return hold; }
public void setFace(int face) { this.face = face; }
public void setFaceString(String faceString) { this.faceString = faceString; }
public void setHold(boolean hold) { this.hold = hold; }
public void roll()
{
int randomNum = 0;
randomNum = MINIMUM + (int)(Math.random() * MAXIMUM);
face = randomNum;
}
@Override
public String toString()
{
return faceString = face + "";
}
}