我是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
。
我认为问题出在参数!
答案 0 :(得分:1)
代码存在问题:
在Java代码中,您尝试打印名称为&#34; id&#34;的请求参数。而不是&#34; sssId&#34;。
此外,您正在尝试使用名为&#34; sssid&#34;的变量。这不是你问题中定义的地方。
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"
最后,Action代码如下所示:
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;
}
}
答案 1 :(得分:0)
如果输入元素具有disabled="disabled"
属性,则在提交表单时不会将其作为参数包含在内。还要重命名与参数名称对应的输入元素名称。 Struts2 getter / setter可用于填充操作bean。
public void setSssId(String id){
this.id = id;
}
答案 2 :(得分:0)
我建议通过javascript检查传递给动作的sssId值。
<button onclick='javascript:submitMyForm();' type="button" value="Envoyer" class="btn btn-success"
>Modifier le serveur</button>
编写以下javascript
function submitMyForm(){
var sssIdElement = document.getElementById("sssId");
document.form_message.action = "modifyServer.action?sssId="+sssIdElement;
document.form_message.submit();
}