Spring - 使用数据绑定重定向到另一个视图

发布于 2024-11-09 07:32:46 字数 1712 浏览 0 评论 0 原文

我认为如果我知道关键字,这个问题就很容易了,但我是 Spring 新手,所以我需要你的帮助,任何编辑都是值得赞赏的。

这就是我从视图中放置和获取对象数据的方法。

@RequestMapping(method = RequestMethod.GET)
public String login(Map model) {
    Member member = new Member(1, "admin", "admin", Boolean.TRUE, null);
    model.put("member", member);
    return "login";
}

// The parameters must be in order @Model, BindingResult, Map
@RequestMapping(method = RequestMethod.POST)
public String ProcessForm(@ModelAttribute("member") Member member, BindingResult result, Map model) {
    dao = new JdbcMemberDao(MemberController.dataSource);
    member = (Member) model.get("member");
    Member tmp = dao.getUser(member.getUsername(), member.getPassword());
    if (tmp != null) {
        model.put("member", tmp);
        return "phonelist";
    }
    return "login";
}

但是,如果我有一个对象列表,那么我如何知道获取我想要的对象:

@RequestMapping(value = "/phonelist", method = RequestMethod.GET)
public String getPhoneList(Map model) {
    List<Phone> phones = dao.getPhones();
    model.put("phones",phones);
    return "phonelist";
}

对象列表视图(使用 JSTL):

<table id="phoneTable">
    <tr><th id="name">Name</th><th id="brand">Brand</th><th id="price">Price</th></tr>
    <c:forEach items="${phones}" var="phone">
    <tr><td><c:out value="${phone.name}"/></td><td><c:out value="${phone.brand}"/></td><td><c:out value="${phone.price}"/></td></tr>
    </c:forEach>
</table>

我的问题是:

  1. 如何为每个对象创建一个带有链接的视图,以便我可以单击特定项目。
  2. 获取我刚刚单击的特定项目。

谢谢十亿:)

I think this question is quite easy if I know the keyword but I'm new to Spring so I need your help, any editing is appreciated.

This is how I can put and get an object data from a view.

@RequestMapping(method = RequestMethod.GET)
public String login(Map model) {
    Member member = new Member(1, "admin", "admin", Boolean.TRUE, null);
    model.put("member", member);
    return "login";
}

// The parameters must be in order @Model, BindingResult, Map
@RequestMapping(method = RequestMethod.POST)
public String ProcessForm(@ModelAttribute("member") Member member, BindingResult result, Map model) {
    dao = new JdbcMemberDao(MemberController.dataSource);
    member = (Member) model.get("member");
    Member tmp = dao.getUser(member.getUsername(), member.getPassword());
    if (tmp != null) {
        model.put("member", tmp);
        return "phonelist";
    }
    return "login";
}

But what if I have a list of objects so how do I know to get the object I want :

@RequestMapping(value = "/phonelist", method = RequestMethod.GET)
public String getPhoneList(Map model) {
    List<Phone> phones = dao.getPhones();
    model.put("phones",phones);
    return "phonelist";
}

the view of list of objects (using JSTL) :

<table id="phoneTable">
    <tr><th id="name">Name</th><th id="brand">Brand</th><th id="price">Price</th></tr>
    <c:forEach items="${phones}" var="phone">
    <tr><td><c:out value="${phone.name}"/></td><td><c:out value="${phone.brand}"/></td><td><c:out value="${phone.price}"/></td></tr>
    </c:forEach>
</table>

My question is:

  1. How can I create a view with link for every object so I can click to a specific item.
  2. get the specific item that I've just clicked.

Thanks a billion :)

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

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

发布评论

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

评论(1

宣告ˉ结束 2024-11-16 07:32:46

Spring 3.1 中通过名为“flash 属性”的新功能解决了这个问题:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-flash-attributes

This is addressed in Spring 3.1 with the new feature called "flash attributes":

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-flash-attributes

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