zoukankan      html  css  js  c++  java
  • Struts 中form表单提交(数据绑定)

    在Struts2中,Form的提交非常方便。

    e.g: A 要在Action中取出页面提交的username和password,两个属性同属User对象,此时:

      在Action中,声明public User user;并给出get()和set();

      在JSP中,

        <input type="text" name="user.username"/>  或用标签:<s:textfield name="user.username" label="用户名"/> 
        <input type="text" name="user.password"/>  或用标签:<s:textfield name="user.password" label="密 码"/>

      这样可将user对象的输入值直接绑定到Action中。

    e.g: B 然而,很多时候我们需要不不仅仅是一个对象,而是多个对象,List,Map,又或者Set。

    先说 Map和List:

       如果 Action 中的属性是 Map<String, User> users; 那么与此对应的表单写法就是:(用标签来写) 
                <s:textfield name="users['one'].username" label="第一个用户名"/> 
                <s:textfield name="users['one'].password" label="第一个密码"/> 
                <s:textfield name="users['two'].username" label="第二个用户名"/> 
                <s:textfield name="users['two'].password" label="第二个密码"/>

      此时,绑定到Action中的就是一个Map类型的users["one","two"]

      如果是对于 Action 中的  List 属性,List<User> users; 那么与此对应的表单写法就是: 
                <s:textfield name="users[0].username" label="第一个用户名"/> 
                <s:textfield name="users[0].password" label="第一个密码"/> 
                <s:textfield name="users[1].username" label="第二个用户名"/> 
                <s:textfield name="users[1].password" label="第二个密码"/> 

      此时,绑定到Action中的就是一个List类型的users[0,1]

    e.g C 我们再来看看Set,set是一个无序集合,所以无法像 List 那样用数字下标来访问

    此时提交页面这么写,最好提交前能根据输入的用户名自动修动输入框的 name。 
            用户名: <input name="users('scott').username"/> 
            密 码: <input name="users('scott').password"/> 
    显示的时候页面可用标签 
            用户名: <s:property value="users('scott').username"/> 
            密 码: <s:property value="users('scott').password"/> 

    <input type="user.username" value="jack"> 
    <input type="user.username" value="lucy"> 
    action中取得username的值是"jack,lucy". 
    注意:此处password是字符串类型,不是数组类型 

    欢迎加入我的QQ群(JAVA开发):216672921,程序 元 世界
  • 相关阅读:
    Codeforces Gym101502 K.Malek and Summer Semester
    Codeforces Gym101502 J-取数博弈
    Codeforces Gym101502 I.Move Between Numbers-最短路(Dijkstra优先队列版和数组版)
    Codeforces Gym101502 H.Eyad and Math-换底公式
    Codeforces Gym101502 F.Building Numbers-前缀和
    Codeforces Gym101502 B.Linear Algebra Test-STL(map)
    Codeforces Gym101502 A.Very Hard Question
    bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块
    Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
    Codeforces Round #250 (Div. 2) A, B, C
  • 原文地址:https://www.cnblogs.com/icerainsoft/p/2321651.html
Copyright © 2011-2022 走看看