gpt4 book ai didi

java - 编辑操作返回 null

转载 作者:行者123 更新时间:2023-12-01 11:41:56 26 4
gpt4 key购买 nike

我是 Struts2 框架的新手,在尝试编辑对象时发现了问题。

我的修改操作的代码:

@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;
}
}

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>
<小时/>

当我执行操作时,request.getParameter 返回null

我认为问题出在参数上!

最佳答案

代码存在问题:

在 Java 代码中,您尝试打印名称为“id”而不是“sssId”的请求参数。

您还尝试使用一个名为“sssid”的变量,该变量在您的问题中没有定义。

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

在 JSP 中,sssId 元素被禁用,当您提交表单时,禁用的元素将被忽略。

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

因此,要获取其值,请在 jsp 中创建一个隐藏元素,并在提交表单时使用 JavaScript 用所需的值更新隐藏元素。

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

在 Javascript 中,它会像:

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

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

最后的操作代码如下所示:

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;

}
}

关于java - 编辑操作返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29456764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com