1

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;
}

public 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 :)

Sara
  • 612
  • 5
  • 21
  • Where are you doing the mapping? – objects Mar 13 '11 at 22:06
  • Are you doing this commando (i.e. raw servlet API), or with a higher-level framework (e.g. spring, struts, etc)? – skaffman Mar 13 '11 at 22:06
  • What do you mean by mapping? Are you trying to get variables to "print" in your form or once the form is submitted you're trying to retrieve the variables and _bind_ them to the object graph? – CarlosZ Mar 13 '11 at 22:07

2 Answers2

2

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I want to use MVC (I've used this pattern in C# a number of times) but it's for an assignment and we have to do it this way :( MVC assignment next :D – Sara Mar 13 '11 at 22:12
  • Yes, I recognize this approach from ASP.NET MVC. In Java, Spring MVC does simliar thing. JSF is a step ahead with UI components. I'd however like to note that using *scriptlets* (those `<% %>` things in JSP) is discouraged since a decade. To read more, have a look at [this question](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files). Instead of that `for` loop for example, you'd like to use JSTL `` tag instead. – BalusC Mar 13 '11 at 22:19
  • Excellent, I'll have a look see. I did wonder if there was a foreach concept in Java. :) Thank you very much for helping me with this! – Sara Mar 13 '11 at 22:44
0

Use things like that:

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

Palesz
  • 2,104
  • 18
  • 20