开发者

JavaBeans being used with complex types

I'm not sure if I've even labelled this question properly. But what I'm trying to do is map a Javabean to form inputs that I have. Except the Bean contains a complex type property that I want to map to (well I want to map to a property of it to be specific). Let me explain with code. I have a game class:

public class Game
{
 private GameStatistics gameStats;
public GameStatistics getGameStats() {

    if(gameStats == null){
     gameStats = new GameStatistics();
    }
    return gameStats;
}

publi开发者_JS百科c void setGameStats(GameStatistics value) {
    gameStats = value;
}
}

and I have a GameStatistics class:

private int amountOfNumbersToMemorise;
public int getAmountOfNumbersToMemorise() {
    return amountOfNumbersToMemorise;
}

public void setAmountOfNumbersToMemorise(String value) {
    amountOfNumbersToMemorise = Integer.parseInt(value);
}

then in my JSP I have something like:

<jsp:useBean id='game' scope='session' class='Classes.Game' />

 <form method="Post" action="numberDisplay.jsp">
      Enter your username:  <input name="username" type="text" /> <br />
      How many numbers to remember: <input name="gameStats.amountOfNumbersToMemorise" type="text"/> <br />
        <input type="Submit" value="Show me the numbers!" />
    </form>

the above page goes to another page:

<jsp:useBean id='game' scope='session' class='Classes.Game' />
<jsp:setProperty name="game" property="*"/>
//more code here
  <ul>
        <% for(int x=0; x < game.getNumbersToMemorise().size(); x++)
            {%>
            <li><%=game.getNumbersToMemorise().get(x) %> </li>
       <% } %></ul>
// more code

There are some properties I haven't included because they aren't important but I can't seem to map the numbers entered into the second input box to a property on the complex type.

I can't use MVC as this is an assignment and we have to do it this way :(

Does this make sense? I'm pretty new to Java (I'm a C# person) so any help would be greatly appreciated, thanks :)


If you're restricted to jsp:useBean/jsp:setProperty, then you need to create the both beans yourself beforehand and then set the child bean as property of the parent bean yourself. The last line in the following snippet does basically a game.setGameStatistics(gameStatistics).

<jsp:useBean id="game" class="com.example.Game" scope="session" />
<jsp:setProperty name="game" property="*" />
<jsp:useBean id="gameStatistics" class="com.example.GameStatistics" scope="request" />
<jsp:setProperty name="gameStatistics" property="*" />
<jsp:setProperty name="game" property="gameStatistics" value="${gameStatistics}" />

Also ensure that the input field names match the exact bean property names. Thus don't use

<input name="gameStats.amountOfNumbersToMemorise">

but just

<input name="amountOfNumbersToMemorise">

and ensure that the both beans doesn't share the same property names.


I'd however warmly recommend to have a look at a Java based MVC framework such as JSF or Spring MVC which does this all transparently without the need to fiddle with legacy jsp:useBean and jsp:setProperty tags.


Use things like that:

<input name="dontworryaboutinputname" type="text" value="${gameStats.amountOfNumbersToMemorise}"/>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜