JavaBeans 与复杂类型一起使用
我不确定我是否正确地标记了这个问题。但我想做的是将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您仅限于
jsp:useBean
/jsp:setProperty
,那么您需要事先自己创建这两个 bean,然后将子 bean 设置为父 bean 的属性你自己。以下代码片段中的最后一行基本上执行game.setGameStatistics(gameStatistics)
。还要确保输入字段名称与 bean 属性名称完全匹配。因此,不要使用
,而只是
确保两个 bean 不共享相同的属性名称。
不过,我强烈建议您查看基于 Java 的 MVC 框架,例如 JSF 或 Spring MVC,它们可以透明地完成这一切,而无需摆弄遗留的
jsp:useBean
和jsp: 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 agame.setGameStatistics(gameStatistics)
.Also ensure that the input field names match the exact bean property names. Thus don't use
but just
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
andjsp:setProperty
tags.使用类似的东西:
;
Use things like that:
<input name="dontworryaboutinputname" type="text" value="${gameStats.amountOfNumbersToMemorise}"/>