2016-04-03 17 views
1

我被一些作业卡住了,我似乎无法找到我正在寻找的答案或将会工作的答案在我的情况。我试着做一个简单的数学测验,点击提交后会检查答案。我开始尝试用jstl来完成这个循环,但似乎无法掌握如何使其工作。我目前的代码是一团糟,我知道可以做得更好,但我正试图在此时获得一个可用的产品。任何帮助是极大的赞赏。JSP/HTML:尝试将html输入传递给具有相同变量名称的多个对象以供使用

我现在正在尝试将我的对象链接到我希望它们使用的输入框,但是因为我的变量名称相同,所有其他名称都被覆盖。我试图让这个工作成功的时候,隔离了两个数学问题。以下代码非常糟糕,但是是我目前使用java和html的技能水平。

的Java bean:

public class MathGen { 

private int a; 
private int b; 
private int c; 
private String problem; 
private String check; 

public MathGen(){ 
    //Generate random numbers a and b 

    int randA = (int) Math.round(Math.random()*100); 
    int randB = (int) Math.round(Math.random()*100); 
    if (randA < randB){ 
     a = randB; 
     b = randA; 
    } 
    else { 
     a = randA; 
     b = randB; 
    } 
    this.problem = (a + " - " + b + " = "); 

} 

public String getProblem() { 

    return problem; 
} 

/** 
* @param c the c to set 
*/ 
public void setC(int c) { 
    this.c = c; 
} 

/** 
* @return the check 
*/ 
public String getCheck() { 
    if ((a - b) != c){ 
     this.check = c + " is INCORRECT"; 
    } 
    else { 
     this.check = c + " is CORRECT"; 
    } 
    return check; 
} 

} 

JSP页面:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 

    <form action="answercheck.jsp" > 


    <jsp:useBean id="problem" scope="session" class="MyClass" /> 
    <jsp:getProperty name="problem" property="problem"/> 
    <input type="text" name="c[]" value="" id="problem" size="2"/><br> 


    <jsp:useBean id="problem1" scope="session" class="MyClass" /> 
    <jsp:getProperty name="problem1" property="problem"/> 
    <input type="text" name="c[]" value="" id="problem1" size="2"/><br> 

    <input type="submit" /> 
    </form> 

回答

0

当你提交一个表单参数是通过名称中使用,所以如果你有两个对象problemproblem1你应该在输入名称中使用对象的名称。

<input type="text" name="problem.c" value="" id="problem" size="2"/><br> 
<input type="text" name="problem1.c" value="" id="problem1" size="2"/><br> 

在另一个jsp中,您可以使用jsp:setProperty从参数中填充bean。

语法的jsp:setProperty行动标签

<jsp:setProperty name="instanceOfBean" property= "*" | 
property="propertyName" param="parameterName" | 
property="propertyName" value="{ string | <%= expression %>}" 
/> 
+0

谢谢。我觉得很愚蠢,我错过了它,现在意识到“参数”标签对用户输入排序的重要性。 – Guardian452

+0

我明白,如果您有更多的问题,请不要忘记回来并提出这个问题。 –

相关问题