在JSP中访问类对象

时间:2014-08-20 22:22:00

标签: java javascript jsp spring-mvc jqote

我正在尝试访问JSP中的对象。

对象是: -

public class Organization implements IOrganization {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "level")
    private Integer level;

    @Column(name = "active")
    private Boolean active;

    @ManyToOne(optional=true, fetch=FetchType.LAZY)
    private Organization parentOrganization;

    @OneToMany(mappedBy="parentOrganization", targetEntity=Organization.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    private Set<Organization> organization = new HashSet<Organization>();

我希望让用户添加一个组织并将父组织显示为下拉列表。我目前正在使用以下代码添加组织。

JS文件 -

function addOrganization(){
    var organization = {};

    organization.id = null,
    organization.name = null;
    organization.level = null;
    organization.active = null;
    organization.parentOrganization = null;


    editRow=$("#organizationEditTemplate").jqote(organization);
    $("#modalMain").empty();
    $("#modalMain").append($(editRow));
    modal.modal({
        keyboard: false,
        backdrop: "static"
    });

    addEditOrganization(organization);
}

function addEditOrganization(organization){
    $('#modalSave').click(function(event){
        console.log("saving organization");
        var inputBoxes = $("#modalBody").children().find("input"),
        issues = false;
        for(var i=0; i< inputBoxes.length; i++){
            var inputBox = $(inputBoxes)[i];
            if($(inputBox).val().length == 0){
                $(issueList).append("<li>" + $(inputBox).prop("placeholder") + " can't be empty");
                $(alertBlock).show();
                issues = true;
            } else if($(inputBox).val().length > 1024){
                $(issueList).append("<li>" + $(inputBox).prop("placeholder") + " can't be larger than 1024 characters");
                $(alertBlock).show();
                issues = true;
            }
        }
        console.log("Are there issues? " + (issues ? "YES" : "NO"));
        if(!issues){
            var editedOrganization = {};
            editedOrganization.id = organization.id ? organization.id : null;
            editedOrganization.name = $(organizationName).val();
            editedOrganization.level = $(organizationlevel).val();
//          editedOrganization.active = $(organizationActive).val();
            //editedOrganization.parentOrganization = $(parentOrganization).val();

            modal.modal('hide');
            modal.children().unbind();
            console.log(editedOrganization);
            if(editedOrganization.id != null){
                saveOrganization(editedOrganization, function(savedOrg){
                    console.log(savedOrg);
                    //updateUser(savedUser);
                    //$(".tablesorter").trigger("update");
                });
            } else {
                //newUserButton.show();
                saveOrganization(editedOrganization, function(savedOrg) {
                    console.log(savedOrg);
                    //insertUser(savedUser);
                    //$(".tablesorter").trigger("update");
                });
            }
        }
    });
}

JSP文件

<script type="x-jqote-template" id="organizationEditTemplate">
            <![CDATA[
                <div class="modal-header">
                    <button type="button" class="close" tabindex="1" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="modalHeader"><#= this.id ? 'Edit Organization' : 'Add Organization' #></h3>
                </div>

                <div class="modal-body" id="modalBody">
                    <div>
                        <div class="span7" style="margin:0px">

                            <div class="control-group" style="margin-top:10px">
                                <label class="span2 control-label" for="organizationName" style="width:auto" >Organization Name:
                                    <input type="text" id="organizationName" placeholder="organization Name" style="margin-top:5px" class="input-small" required="required" value="<#= this.name ? this.name : '' #>"/>
                                    <ul style="display:none"></ul>
                                </label>
                            </div>

                            <div class="control-group" >
                                <label class="span2 control-label" for="parentOrganization" style="width:auto" >Parent Organization:
                                    <input type="text" id="parentOrganization" placeholder="Parent Organization" style="margin-top:5px" class="input-small" value="<#= this.parentOrganization ? this.parentOrganization : '' #>"/>
                                    <ul style="display:none"></ul>
                                </label>
                            </div>

                        </div>
                    </div>

                    <div>
                            <div class="span4" >
                                <label class="control-label" style="width:auto" for="organizationlevel">level:
                                    <select class="organizationlevel span2" style="margin-left:5px; margin-top:5px" id="organizationlevel">
                                        <option selected="selected" id="organizationlevel-select" value="-1">-- Select --</option>
                                        <option id="organizationlevel-L1" value= 1 >L1</option>
                                        <option id="organizationlevel-L2" value= 2 >L2</option>
                                    </select>
                                </label>
                            </div>  
                    </div>

                    <div class="row" style="margin-left:0px;">
                        <div class="span4" >
                            <label class="control-label" style="width:auto" for="organizationActive">Active:
                                <select class="organizationActive span2" style="margin-left:5px; margin-top:5px" id="organizationActive">
                                    <option selected="selected" id="OrganizationActive-select" value="-1">-- Select --</option>
                                    <option id="OrganizationActive-Q1" value = 1 >Yes</option>
                                    <option id="OrganizationActive-Q2" value = 0 >No</option>
                                </select>
                            </label>
                        </div>
                    </div>

                    </div>

                <div class="modal-footer">
                    <button class="btn" id="modalSave" tabindex="1" aria-hidden="true">Save</button>
                    <button class="btn" id="modalClose" tabindex="1" data-dismiss="modal" aria-hidden="true">Close</button>
                </div>
            ]]>
            </script>

这就是我从控制器

设置modelAttribute的方法
@RequestMapping(value="")
    public String resourceMain(Model uiModel) {
        List<Organization> organization = new ArrayList<Organization>();


        logger.info("About to direct to emp view");
        List<Organization> organizations = service.list();
        logger.trace("Organizations listed: " + organizations.size());
        for(Organization org : organizations) {
            logger.trace(org.toString());
        }
        uiModel.addAttribute("organizations", organizations);

        return "organization/organization";
    }   

从实体到模型的映射 -

@Transactional(readOnly=true)
public List<Organization> list() {
    TypedQuery<Organization> query = em.createQuery("from Organization e", Organization.class);
    return query.getResultList();
}

请建议如何访问JSP文件中的父组织对象。

0 个答案:

没有答案