对于我在IT大学中的一个项目,我必须在Strut2应用程序中创建排雷器游戏。对于服务器端,我使用Tomcat。
您现在可以看到游戏的外观:
当我单击一个框时,我们必须看到一个数字或一个地雷。为此,有两个参数(X和Y)用于更新单击框的状态。为了使框可点击,我使用代码:
<a href="
<s:url action='demineur' method="connexion">
<s:param name="x" value="#x.index"/>
<s:param name="y" value="#y.index"/>
</s:url>
">
[当我们想玩这个游戏时,必须输入用户名:
[登录游戏时,我们会创建一个以玩家名字命名的会话,并且在该会话中我们会保存游戏。
并且当我们与游戏互动时,游戏的保存即为更新
用老师给的API(ModeleDemineur:1.1)创建一个地雷,并像它的显示一样显示::>
<table>
<s:iterator status="x" var="ligne" value="cases">
<tr>
<s:iterator status="y" var="case" value="ligne">
<td>
<s:if test="getCachee()">
<a href="
<s:url action='demineur' method="connexion">
<s:param name="x" value="#x.index"/>
<s:param name="y" value="#y.index"/>
</s:url>
">
<img src="${pageContext.request.contextPath}/img/cellCovered.png"/>
</a>
</s:if>
<s:else>
<s:if test="getValeur() == -1">
<img src="${pageContext.request.contextPath}/img/mineExploded.png"/>
</s:if>
<img src="${pageContext.request.contextPath}/img/box<s:property value='getValeur()'/>.png"/>
</s:else>
</td>
</s:iterator>
</tr>
</s:iterator>
</table>
public class ActionDemineur extends ActionSupport implements SessionAware {
String username;
GestionDemineur demineur;
Plateau plateau;
Case[][] cases;
String val;
String x,y;
private Map<String, Object> session;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public GestionDemineur getDemineur() {
return demineur;
}
public void setDemineur(GestionDemineur demineur) {
this.demineur = demineur;
}
public Plateau getPlateau() {
return plateau;
}
public void setPlateau(Plateau plateau) {
this.plateau = plateau;
}
public Case[][] getCases() {
return cases;
}
public void setCases(Case[][] cases) {
this.cases = cases;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
@Override
public void setSession(Map<String, Object> map) {
this.session = map;
}
public Map<String, Object> getSession() {
return session;
}
public String connexion() throws ExceptionLoginDejaPris {
if(!session.containsKey("user"))
{
if(username != null)
{
session.put("user", username);
}
else {
return INPUT;
}
}
if(!session.containsKey("plateau")) {
System.err.println("plateau not found");
demineur = new GestionDemineur();
demineur.connexion(username);
plateau = demineur.getPlateau(username);
cases = plateau.getMonPlateau();
session.put("plateau", plateau);
session.put("user", username);
}
username = (String) session.get("user");
plateau = (Plateau) session.get("plateau");
cases = plateau.getMonPlateau();
System.err.println(plateau);
if(x != null && y != null)
{
System.err.println("x and y not null");
System.err.println(Integer.parseInt(x) + " -- " + Integer.parseInt(y));
try {
plateau.decouvrirCase(Integer.parseInt(x), Integer.parseInt(y));
} catch (BombeException e) {
session.remove("plateau", plateau);
session.remove("user", username);
return "perdu";
}
System.err.println(plateau.getMonPlateau()[Integer.parseInt(x)][Integer.parseInt(y)]);
}
return SUCCESS;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<package name="/" extends="struts-default">
[...]
<action name="demineur" class="struts.ActionDemineur" method="connexion">
<result name="input">/WEB-INF/pages/connexionDemineur.jsp</result>
<result name="success">/WEB-INF/pages/demineur.jsp</result>
<result name="perdu">/WEB-INF/pages/perduDemineur.jsp</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.univ-orleans.ufrst.info.l3.pnt</groupId>
<artifactId>HelloStruts2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.22</version>
</dependency>
[...]
<dependency>
<groupId>fr.miage.orleans.tp</groupId>
<artifactId>ModeleDemineur</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>13</source>
<target>13</target>
<encoding>UTF-8</encoding>
</configuration>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.25.v20191220</version>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
</project>
ERROR ParametersInterceptor Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'x' on 'class com.opensymphony.xwork2.ActionSupport: Error setting expression 'x' with value ['0', ]
ERROR ParametersInterceptor Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'y' on 'class com.opensymphony.xwork2.ActionSupport: Error setting expression 'y' with value ['4', ]
我曾尝试在[[struts.xml中添加xml <interceptor-ref name=params />
行,但他删除了会话中具有游戏保存和当前用户名的数据。
感谢您的未来帮助
简介对于我IT大学的一个项目,我必须在Strut2应用程序中创建排雷器游戏。对于服务器端,我使用Tomcat。您可以看到当前游戏的外观:当我...