2016-04-03 35 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

我明白,如果您有更多的問題,請不要忘記回來並提出這個問題。 –

相關問題