abdel
abdel

Reputation: 53

Edit action returns null

I'm new to Struts2 framework and I found a problem when I try to edit an object.

The code of my modification action:

@Action(value = "modifyServer", results = {
        @Result(name = "success", location = Location.MAIN_PAGE),
        @Result(name = "error", location = Location.LOGIN_PAGE) })
public String modifyServer() {
    em = DbConnexion.getEntityManager().createEntityManager();
    String id=request.getParameter(sssid);
    logger.info("id serveur         = "+request.getParameter("id")); 
    try {
        em.getTransaction().begin();
        Simserver server = em.find(Simserver.class, id);
        server.setSssServer(request.getParameter("sssServer"));
        server.setSssIp(request.getParameter("sssIp"));
        server.setSssPort(request.getParameter("sssPort"));
        em.getTransaction().commit();
        System.out.println("modification done !!!");
        em.close();
        return SUCCESS;
    } catch (Exception e) {
        return ERROR;
    }
}

The JSP:

   <form class="form-horizontal" action="modifyServer" role="form"
name="form_message" method="get">
<div id="elmsg"></div>
<div class="panel panel-info">
    <div class="panel-heading expand" id="second-level">
        <h6 class="panel-title">Modification du Serveur</h6>
    </div>
    <div class="panel-body">
        <div class="form-group">
            <label class="col-sm-2 control-label"> Id du Serveur : <span
                class="mandatory">*</span></label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="sssId"
                    disabled="disabled" id="sssId"
                    value="<s:property value="#request.curentserver.sssId" />">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label"> Nom du Serveur : <span
                class="mandatory">*</span></label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="sssServer"
                    id="sssServer"
                    value="<s:property value="#request.curentserver.sssServer" />">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"> Adresse IP : <span
                class="mandatory">*</span></label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="sssIp" id="sssIp"
                    value="<s:property value="#request.curentserver.sssIp" />" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"> Port : <span
                class="mandatory">*</span></label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="sssPort" id="sssPort"
                    value="<s:property value="#request.curentserver.sssPort" />" />
            </div>
        </div>

        <div class="form-actions text-right">
            <button type="submit" value="Envoyer" class="btn btn-success"
                >Modifier le serveur</button>
            <a role="button" href="gestionServeurList" class="btn btn-danger">Retour
                à la list des serveurs</a>
        </div>

When I execute my action the request.getParameter returns null.

I think the issue is in the parameter!

Upvotes: 1

Views: 453

Answers (3)

Learner
Learner

Reputation: 21425

There are issues with code:

In Java code you are trying to print a request parameter with name as "id" instead of "sssId".

Also you are trying to use a variable called "sssid" that is no where defined in your question.

String id=request.getParameter(sssid);
logger.info("id serveur  = "+request.getParameter("id"));

In JSP the sssId element is disabled, when you submit a form the disabled elements are ignored.

<input type="text" class="form-control" 
    name="sssId" disabled="disabled" id="sssId"
    value="<s:property value="#request.curentserver.sssId" />">

So to get its value, create a hidden element in your jsp and on form submission update the hidden element with the required value using JavaScript.

<input type="hidden" name="sssId" value=""/>

In Javascript it will be like:

document.getElementById("sssId").value = 123; // Give value here

document.getElementById("myForm").submit(); // Give an Id to your form, say "myForm"

Finally the Action code looks like this :

public class MyAction extends ActionSupport implements ServletRequestAware {
    @Action(value = "modifyServer", results = {
            @Result(name = "success", location = Location.MAIN_PAGE),
            @Result(name = "error", location = Location.LOGIN_PAGE) })
    public String modifyServer() {
        String id = request.getParameter("sssId");
        System.out.println("id serveur = " + id);
        return null;
    }

    private HttpServletRequest request;

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;

    }
}

Upvotes: 0

Code2Interface
Code2Interface

Reputation: 495

I'd suggest checking value of sssId being passed to the action through javascript.

 <button onclick='javascript:submitMyForm();' type="button" value="Envoyer" class="btn btn-success"
                >Modifier le serveur</button>

write the following javascript

  function submitMyForm(){
    var sssIdElement  = document.getElementById("sssId");
    document.form_message.action = "modifyServer.action?sssId="+sssIdElement;
    document.form_message.submit();
  }

Upvotes: 0

Roman C
Roman C

Reputation: 1

If the input element has disabled="disabled" attribute it won't include as parameter when your form is submitted. Also rename the input element name that correspond to a parameter name. Struts2 getter/setter can be used to populate the action bean.

public void setSssId(String id){
  this.id = id;
}

Upvotes: 0

Related Questions