struts2 后台获取前台传入的值获取不到,请帮忙看看哪里错了?

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'addProject.jsp' starting page</title>
<script type="text/javascript">
function saveToMain(){
var flag =true;
var programId = document.getElementById("np.programId").value;
var programName = document.getElementById("np.programName").value;

if(programId == ""){
alert("项目编号不能为空");
flag = false;
return;
}else if(programName == ""){
alert("项目名称不能为空");
flag = false;
return;
}

if(flag ==true){
document.forms[0].action = "/testStructs2/test/addNewProject.action";
document.forms[0].submit();
}
}
</script>
</head>
<body>
<form id="myForm" name="myForm" method="post">
<table width="926px">
<tr class="tb_b">
<td ><img src="images/main01_14.gif" width="13" height="13" /> <strong>当前位置:项目管理 >> 新增项目</strong></td>
<td width="37%" style="text-align:right; padding-right:30px;"><img src="images/045631333.gif" width="16" height="16" />
<a href="/testStructs2/test/addSub.action">添加</a>
<a href="javascript:saveToMain();"><img src="images/send.gif" width="16" height="16" />保存</a></td>
</tr>
<tr style="text-align:left; background-image:url(images/tabbar_mid.gif); height:25px; font-size:14px; color:white;">
<td colspan="2"><img src="images/right_iocn.jpg" /> <strong>新增项目</strong></td>
</tr>
<tr >
<td colspan="2">
<table width="926px" bordercolor="#CCCCCC" border="1px" style="border-collapse:collapse;margin-top:5px;">
<tr>
<td width="29%" class="td_bgs_1"><strong>项目编号</strong></td>
<td width="71%" style=" padding-left:5px;"><input type="text" name = "np.programId" size = "50" /></td>
</tr>
<tr>
<td width="29%" class="td_bgs_1"><strong>项目名称:</strong></td>
<td width="71%" style=" padding-left:5px;"><input type="text" name="np.programName" size="50" /></td>
</tr>
<tr>
<td class="td_bgs_1"><strong>分包一名称</strong>:</td>
<td style=" padding-left:5px;"><input type="text" name="np.project1" size="50" /></td>
</tr>
<tr>
<td class="td_bgs_1"><strong>分包二名称:</strong></td>
<td style=" padding-left:5px;"><input type="text" name="np.project2" size="50" /></td>
</tr>
</table>
</td>
</tr>

</table>

</form>
</body>
</html>
ACTION
public class addNewProject {
public String execute(){
NewProject np = new NewProject();
np.setProgramId(np.getProgramId());
np.setProgramName(np.getProgramName());
np.setProject1(np.getProject1());
np.setProject2(np.getProject2());

System.out.println(np.getProgramId());
System.out.println(np.getProgramName());
return "success";
}
}

第1个回答  2012-07-31
你写的action里有问题啊
np.setProgramId(np.getProgramId());
np.setProgramName(np.getProgramName());
np.setProject1(np.getProject1());
np.setProject2(np.getProject2());

这怎么能获取到传的值啊。把它改成
np.setProgramId(this.getProgramId());
np.setProgramName(this.getProgramName());
np.setProject1(this.getProject1());
np.setProject2(this.getProject2());

需要定义一个变量,然后在添加set,get方法。像下面那样
private String ProgramId;
public String getProgramId(){
return ProgramId ;}
public void setProgramId(String ProgramId){
this.ProgramId=ProgramId;
}
其他类似,这里我就不写了,很简单的。
像你那样是获取不到值的追问

我在NewProject 类中定义了
private String programId;
private String programName;
private String project1;
private String project2;
并且添加了set和get方法,这样也不行吗?

追答

知道this的含义吗?
np.setProgramId(this.getProgramId());
np.setProgramName(this.getProgramName());
np.setProject1(this.getProject1());
np.setProject2(this.getProject2());

np.setProgramId(np.getProgramId());
np.setProgramName(np.getProgramName());
np.setProject1(np.getProject1());
np.setProject2(np.getProject2());
是不一样的,this.getProgramName()是获取传递过来的值,而那个np.getProgramName()起初是空的

本回答被提问者采纳

struts2 后台获取前台传入的值获取不到,请帮忙看看哪里错了?
这怎么能获取到传的值啊。把它改成 np.setProgramId(this.getProgramId());np.setProgramName(this.getProgramName());np.setProject1(this.getProject1());np.setProject2(this.getProject2());需要定义一个变量,然后在添加set,get方法。像下面那样 private String ProgramId;public String g...

servlet或者struts2,strut等获取前台jsp中text(不仅text)自定义属性...
这个值直接获取不到,除非在向后台提交请求之前手动取获取,然后绑到请求上带到后台

相似回答