JavaBeans 与复杂类型一起使用

发布于 2024-10-21 14:35:48 字数 1774 浏览 1 评论 0原文

我不确定我是否正确地标记了这个问题。但我想做的是将 Javabean 映射为我拥有的输入。除了 Bean 包含一个我想要映射到的复杂类型属性(具体来说,我想映射到它的一个属性)。我用代码来解释一下。我有一个游戏类:

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

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

public void setGameStats(GameStatistics value) {
    gameStats = value;
}
}

并且我有一个 GameStatistics 类:

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

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

然后在我的 JSP 中我有类似的内容:

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

上面的页面转到另一页:

<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

有一些属性我没有包含,因为它们并不重要,但我似乎无法将输入到第二个输入框中的数字映射到复杂类型的属性。

我不能使用 MVC,因为这是一项作业,我们必须这样做:(

这有意义吗?我对 Java 很陌生(我是 C# 人员),所以任何帮助将不胜感激,谢谢:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椒妓 2024-10-28 14:35:48

如果您仅限于 jsp:useBean/jsp:setProperty,那么您需要事先自己创建这两个 bean,然后将子 bean 设置为父 bean 的属性你自己。以下代码片段中的最后一行基本上执行 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}" />

还要确保输入字段名称与 bean 属性名称完全匹配。因此,不要使用

<input name="gameStats.amountOfNumbersToMemorise">

,而只是

<input name="amountOfNumbersToMemorise">

确保两个 bean 不共享相同的属性名称。


不过,我强烈建议您查看基于 Java 的 MVC 框架,例如 JSF 或 Spring MVC,它们可以透明地完成这一切,而无需摆弄遗留的 jsp:useBeanjsp: setProperty 标签。

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.

清泪尽 2024-10-28 14:35:48

使用类似的东西:

;

Use things like that:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文