简体   繁体   English

如何使用显示标记将要删除/编辑的行中的值从struts2中的jsp传递到动作类?

[英]How to pass the values in the row that i want to delete/edit to the action class from jsp in struts2 using display tag?

I want to create edit , delete link in display tag with struts2. 我想在显示标签中使用struts2创建edit,delete链接。 This is my jsp code 这是我的jsp代码

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>
<html>
<head>

    <title>Contact Manager - display tag Example</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<display:table  name="contactList" requestURI="" pagesize="10" export="true" cellpadding="1" uid="sr"  cellspacing="1" size="50"
    defaultorder="ascending" sort="list" style="width:850"
     id="row">
            <display:column property="id" title="serial no" sortable="true"   />
            <display:column property="lastName" title="TV Show" sortable="true"   />
            <display:column property="firstName" title="User Name" sortable="true"  />
            <display:column property="emailId" title="Email Id" sortable="true"  />

            <display:column media="html"
    title="Delete"
    style="text-align:center">
    <s:url id="deleteUrl" action="deleteLink">
    <s:param name="id1" value="#attr.row.id" />
    </s:url>
    <s:a href="%{deleteUrl}">
         Delete
    </s:a>
</display:column>

<display:column media="html"
title="edit"
style="text-align:center">
<a href="deleteLink?id=<s:property value="id"/>">edit</a>
</display:column>

             <display:setProperty name="export.excel.filename" value="ActorDetails.xls"/>
            <display:setProperty name="export.pdf.filename" value="ActorDetails.pdf"/>
            <display:setProperty name="export.pdf" value="true" />
        </display:table>



</body>
</html>

this is my action class: 这是我的动作课:

    package net.viralpatel.contact.view;

    import java.util.List;

    import net.viralpatel.contact.controller.ContactManager;
    import net.viralpatel.contact.model.Contact;

    import com.opensymphony.xwork2.ActionSupport;


    public class ContactAction extends ActionSupport implements ModelDriven<Contact>,Preparable {

        private static final long serialVersionUID = 9149826260758390091L;

        private Contact contact;
        private List<Contact> contactList;


        private ContactManager linkController;

        public ContactAction() {
            linkController = new ContactManager();
        }



        public String getRecords()
        {
            contactList =linkController.lSist();

            return SUCCESS;
        } 

        public String deleteRecord()
        {
           System.out.println(contact.getId());

            return SUCCESS;
        } 

        public Contact getContact() {
            return contact;
        }

        public List<Contact> getContactList() {
            return contactList;
        }

        public void setContact(Contact contact) {
            this.contact = contact;
        }

        public void setContactList(List<Contact> contactsList) {
            this.contactList = contactsList;
        }


@Override
    public Contact getModel() {

        return contact;
    }

    @Override
    public void prepare() throws Exception {
        // TODO Auto-generated method stub
        contact = new Contact();
    }
    }

How do I get the values of the attributes in the row which I delete from the jsp page to the deleteRecord() method in the ContactAction class?? 如何获取我从jsp页面删除到ContactAction类中的deleteRecord()方法的行中的属性值? HERE the statement 这里的声明

System.out.println("ID IS "+contact.getId());

gives an output: ID IS null 给出输出:ID IS为null

Please suggest some solution. 请提出一些解决方案。

Thanks 谢谢

this is the Contact bean 这是联络豆

package net.viralpatel.contact.model;

import java.io.Serializable;
import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Contacts")
public class Contact implements Serializable{

    private static final long serialVersionUID = -8767337896773261247L;

    private Long id;
    private String firstName;
    private String lastName;
    private String emailId;
    private String cellNo;
    private Date birthDate;
    private String website;

    private Date created;

    @Id
    @GeneratedValue
    @Column(name="id")
    public Long getId() {
        return id;
    }
    @Column(name="firstname")
    public String getFirstName() {
        return firstName;
    }
    @Column(name="lastname")
    public String getLastName() {
        return lastName;
    }
    @Column(name="email_id")
    public String getEmailId() {
        return emailId;
    }
    @Column(name="cell_no")
    public String getCellNo() {
        return cellNo;
    }
    @Column(name="birthdate")
    public Date getBirthDate() {
        return birthDate;
    }
    @Column(name="website")
    public String getWebsite() {
        return website;
    }
    @Column(name="created")
    public Date getCreated() {
        return created;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }
    public void setCellNo(String cellNo) {
        this.cellNo = cellNo;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    public void setWebsite(String website) {
        this.website = website;
    }
}

You can use 您可以使用

#attr.uidOfYourTable.property

in your case 在你的情况下

<display:column media="html" title="edit" style="text-align:center">
   <a href="deleteLink?id=<s:property value="#attr.sr.id"/>">edit</a>
</display:column>

(leaving out the fact that your Edit link points to an DeleteLink Action :o) (忽略了您的“编辑”链接指向一个DeleteLink Action的事实:)

Here's what the documentation says about the display:table tag: 这是文档中关于display:table标签的内容:

id: See "uid". id:参见“ uid”。

uid: Unique id used to identify this table. uid:用于标识此表的唯一ID。 The object representing the current row is also added to the pageContext under this name, so that you can refer to it in column bodies using ${uid} . 代表当前行的对象也以该名称添加到pageContext中,以便您可以使用$ {uid}在列主体中引用它 You can also use uid_rowNum to refer to the current row number. 您也可以使用uid_rowNum引用当前行号。 Two tables in the same page can't have the same id (paging and sorting will affect both). 同一页面中的两个表不能具有相同的ID(分页和排序会同时影响这两个表)。 If no "htmlId" is specified the same value will be used for the html id of the generated table. 如果未指定“ htmlId”,则将相同的值用于生成表的html id。

(emphasis mine) (强调我的)

So choose between id and uid (and don't specify the id twice as you're doing) in your tag instead of using both, and then (assuming you keep uid), use ${sr} to refer to the current element: 因此,请在标签中的iduid之间选择(并且不要同时指定两次id),而不要同时使用二者,然后(假设您保留uid),请使用$ {sr}来引用当前元素:

<s:param name="id1" value="${sr.id}" />

Here, is the answer we can use the <s:bean> to instantiate the bean class and then <s:param> acts as the setter method whose values can be retrieved in the action class using getter methods of the bean class Contact 这是答案,我们可以使用<s:bean>实例化bean类,然后<s:param>充当setter方法,可以使用bean类Contact的getter方法在操作类中检索其值。

<display:column media="html"
    title="Delete"
    style="text-align:center">
    <s:url id="deleteUrl" action="deleteLink">
    <s:bean name="net.viralpatel.contact.model.Contact"></s:bean>
    <s:param name="id" value="#attr.row.id"></s:param>
    <s:param name="firstName" value="#attr.row.firstName"></s:param>
    </s:url>
    <s:a href="%{deleteUrl}">
         Delete
    </s:a>
</display:column>

你可以这样写:

     <display:column property="status" title="Status" sortable="true" paramId="aid" paramProperty="aid" href="/MsgSystem/adminread" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM