将从 jsp 中选择的值传递给 struts 中的操作
Passing value selected from jsp to Action in struts
我最近尝试学习 java 和 struts。
我想从我的下拉列表中传递选定的值,但我在我的操作中收到的所有内容都是 null
这是我的代码
我的 jsp
中的下拉菜单
<s:select list="UrgencyList" listKey="name" listValue="name"
key="urgency" name="urgency" emptyOption="false" headerKey="12" required="true"
headerValue="--------------------------------------------------------" />
<font class="ui-widget-R"><s:property value="errors['urgency'][0]" /></font>
我的 xml,SavSharing
是我的按钮
使用的 post 动作
<action name="SavSharing" class="com.ao.qshare.form.Sharing" method="Save">
<exception-mapping exception="org.apache.commons.fileupload.FileSizeLimitExceededException"
result="error" />
<result name="success">/qs/sharing.jsp</result>
<result name="input">/qs/sharing.jsp</result>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="mydefault" />
</action>
public class Sharing extends ActionSupport {
public String Save() throws Exception {
String result = "";
SharingSqlManger sql = null;
SiteDeploySqlManger sSql=null;
try {
Log.info(getClass(), "-----Save start-----");
con = DbConnection.getDbConnection();// get account
con.setAutoCommit(false);
sql=new SharingSqlManger(con);
sSql= new SiteDeploySqlManger(con);
ActionContext actionContext = ActionContext.getContext();
Map<?,?> data=actionContext.getParameters();
String tp=SharingUtil.getString(data.get("tp"));
attachs=SharingUtil.getFile(this.sno,SharingConstants.sharing);
boolean isSameFile=SharingUtil.isSameFile(SharingUtil.getFileString(attachs),this.getUploadFileName());
// boolean isSameFile=SharingUtil.isSameFile(this.attach,this.getUploadFileName());
//save
this.setPara();
String oldStatus=entity.getStatus();
if (isSameFile){
this.addActionMessage(SharingConstants.messFileExists);
mess.add(SharingConstants.messFileExists);
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}else{
//upload!=null,do upload
if (!GenericValidator.isBlankOrNull(this.getUploadFileName())) {
this.setAttach(this.attach+this.getUploadFileName()+";");
entity.setAttach(this.attach);
}
if (tp.equals("File")){
if (this.lengthValidate() && this.isSaveValidate(sql)){
if (status.equals(SharingConstants.deployStatusKey)) {
SharingEntity shEntity = sql.getSharing(sno, "</p>");
entity.setAlertSite(shEntity.getAlertSite());
entity.setAlertDept(shEntity.getAlertDept());
entity.setDeployDept(shEntity.getDeployDept());
entity.setDeploySite(shEntity.getDeploySite());
entity.setDSite(shEntity.getDSite());
entity.setDDept(shEntity.getDDept());
//Start CR-2014-01727 Kevin
entity.setAlertCopy(shEntity.getAlertCopy());
entity.setDeployCopy(shEntity.getDeployCopy());
//End CR-2014-01727 Kevin
}
//save
sno = sql.save(entity,SharingConstants.saveDocument,SharingConstants.sharing,oldStatus);
this.setSno(sno);
entity=sql.getSharing(sno,"</p>");
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
if (upload != null)
SharingUtil.getFile(upload, this.getUploadFileName(), sno,
SharingConstants.sharing);
init(sql);
con.commit();
Log.info(getClass(), "-----Save end-----");
isValidate=true;
result = SUCCESS;
}else{
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}
}else{
if (this.status.equals(SharingConstants.newStatusKey)){
this.setStatus(SharingConstants.drafStatusKey);
if (this.lengthValidate() && this.isSaveValidate(sql)){
entity.setStatus(SharingConstants.drafStatusKey);
//save
sno = sql.save(entity,SharingConstants.saveDocument,SharingConstants.sharing,oldStatus);
Log.info(getClass(), sno + " status:"+this.getStatusName());
Log.info(getClass(), sno + " save finished");
this.setSno(sno);
entity=sql.getSharing(sno,"</p>");
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
con.commit();
if (upload != null)
SharingUtil.getFile(upload, this.getUploadFileName(), sno,
SharingConstants.sharing);
Log.info(getClass(), "-----Save end-----");
isValidate=true;
result = SUCCESS;
}else{
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}
}else{
if (status.equals(SharingConstants.deployStatusKey)){//deploy
if (this.lengthValidate() && isValidate(SharingConstants.Deployed,sql)) {
SharingEntity shEntity=sql.getSharing(sno,"</p>");
entity.setAlertSite(shEntity.getAlertSite());
entity.setAlertDept(shEntity.getAlertDept());
entity.setDeployDept(shEntity.getDeployDept());
entity.setDeploySite(shEntity.getDeploySite());
entity.setDSite(shEntity.getDSite());
entity.setDDept(shEntity.getDDept());
//Start CR-2014-01727 Kevin
entity.setAlertCopy(shEntity.getAlertCopy());
entity.setDeployCopy(shEntity.getDeployCopy());
//End CR-2014-01727 Kevin
sno=sql.save(entity,SharingConstants.saveDocument,SharingConstants.sharing,oldStatus);
this.setSno(sno);
entity=sql.getSharing(sno,"</p>");
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
con.commit();
if (upload != null)
SharingUtil.getFile(upload, this.getUploadFileName(), sno,
SharingConstants.sharing);
Log.info(getClass(), "-----Save end-----");
isValidate=true;
result = SUCCESS;
}else{
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}
}else{
if (this.lengthValidate() && isSaveValidate(sql)) {
sno=sql.save(entity,SharingConstants.saveDocument,SharingConstants.sharing,oldStatus);
this.setSno(sno);
entity=sql.getSharing(sno,"</p>");
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
con.commit();
if (upload != null)
SharingUtil.getFile(upload, this.getUploadFileName(), sno,SharingConstants.sharing);
Log.info(getClass(), "-----Save end-----");
isValidate=true;
result = SUCCESS;
}else{
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}
}
}
}
}
if (!tp.equals("Exit")){
isValidate=false;
}
} catch (SizeLimitExceededException e) {
Log.info(this.getClass(), "SizeLimitExceededException:" + e.getMessage());
mess.add("SizeLimitExceededException:"+e.getMessage());
this.setStatusName(SharingConstants.New);
result=INPUT;
if (con!=null) con.close();
} catch (IOException e) {
Log.info(this.getClass(), "IOException:" + e.getMessage());
mess.add("IOException:"+e.getMessage());
this.setStatusName(SharingConstants.New);
result=INPUT;
if (con!=null) con.close();
} catch (FileUploadException e) {
Log.info(this.getClass(), "FileUploadException:" + e.getMessage());
// this.addActionError("Exception:"+e.getMessage());
mess.add("FileUploadException:"+e.getMessage());
this.setStatusName(SharingConstants.New);
result=INPUT;
if (con!=null) con.close();
} catch (Exception e) {
Log.info(this.getClass(), "Save exception:" + e.getMessage());
mess.add("Exception:"+e.getMessage());
this.setStatusName(SharingConstants.New);
result=INPUT;
// this.addActionError("Exception:"+e.getMessage());
if (con!=null) con.close();
}finally{
// this.setEntityDesc(sql,sSql);
// this.setEntity(entity);
//
// init(sql);
// isValidate=false;
if (con!=null) con.close();
}
return result;
}
private String urgency="";
public String geturgency() {
return urgency;
}
public void seturgency(String urgency) {
this.urgency = urgency;
}
}
我认为我应该从 jsp 中获得价值,但没有按照我的预期去做。
注意:我在上面的代码中删除了其他文本字段和下拉菜单,除了我添加的紧急下拉菜单外,所有这些都有效。
在调试模式的控制台上也没有发现异常或错误
您的 urgency
setter 名称不正确;它们应该遵循 JavaBean 命名约定并且是 setUrgency
/getUrgency
.
不相关:
- 操作方法应命名为
save
以遵循 Java 命名约定。
- 所有参数应通过setters(或
ModelDriven
)检索,无需在此处访问操作上下文。
- 您的大部分异常处理都可以通过单个 multi-catch 处理。
- 此操作在单个方法中做得很远,很远太多了:重构。
- 一般来说,setters 应该设置即可。如果 setter 做的不仅仅是设置,它应该有一个更明显的名字。
还有更多,但现在已经足够了:)
我最近尝试学习 java 和 struts。 我想从我的下拉列表中传递选定的值,但我在我的操作中收到的所有内容都是 null
这是我的代码
我的 jsp
中的下拉菜单<s:select list="UrgencyList" listKey="name" listValue="name"
key="urgency" name="urgency" emptyOption="false" headerKey="12" required="true"
headerValue="--------------------------------------------------------" />
<font class="ui-widget-R"><s:property value="errors['urgency'][0]" /></font>
我的 xml,SavSharing
是我的按钮
<action name="SavSharing" class="com.ao.qshare.form.Sharing" method="Save">
<exception-mapping exception="org.apache.commons.fileupload.FileSizeLimitExceededException"
result="error" />
<result name="success">/qs/sharing.jsp</result>
<result name="input">/qs/sharing.jsp</result>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="mydefault" />
</action>
public class Sharing extends ActionSupport {
public String Save() throws Exception {
String result = "";
SharingSqlManger sql = null;
SiteDeploySqlManger sSql=null;
try {
Log.info(getClass(), "-----Save start-----");
con = DbConnection.getDbConnection();// get account
con.setAutoCommit(false);
sql=new SharingSqlManger(con);
sSql= new SiteDeploySqlManger(con);
ActionContext actionContext = ActionContext.getContext();
Map<?,?> data=actionContext.getParameters();
String tp=SharingUtil.getString(data.get("tp"));
attachs=SharingUtil.getFile(this.sno,SharingConstants.sharing);
boolean isSameFile=SharingUtil.isSameFile(SharingUtil.getFileString(attachs),this.getUploadFileName());
// boolean isSameFile=SharingUtil.isSameFile(this.attach,this.getUploadFileName());
//save
this.setPara();
String oldStatus=entity.getStatus();
if (isSameFile){
this.addActionMessage(SharingConstants.messFileExists);
mess.add(SharingConstants.messFileExists);
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}else{
//upload!=null,do upload
if (!GenericValidator.isBlankOrNull(this.getUploadFileName())) {
this.setAttach(this.attach+this.getUploadFileName()+";");
entity.setAttach(this.attach);
}
if (tp.equals("File")){
if (this.lengthValidate() && this.isSaveValidate(sql)){
if (status.equals(SharingConstants.deployStatusKey)) {
SharingEntity shEntity = sql.getSharing(sno, "</p>");
entity.setAlertSite(shEntity.getAlertSite());
entity.setAlertDept(shEntity.getAlertDept());
entity.setDeployDept(shEntity.getDeployDept());
entity.setDeploySite(shEntity.getDeploySite());
entity.setDSite(shEntity.getDSite());
entity.setDDept(shEntity.getDDept());
//Start CR-2014-01727 Kevin
entity.setAlertCopy(shEntity.getAlertCopy());
entity.setDeployCopy(shEntity.getDeployCopy());
//End CR-2014-01727 Kevin
}
//save
sno = sql.save(entity,SharingConstants.saveDocument,SharingConstants.sharing,oldStatus);
this.setSno(sno);
entity=sql.getSharing(sno,"</p>");
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
if (upload != null)
SharingUtil.getFile(upload, this.getUploadFileName(), sno,
SharingConstants.sharing);
init(sql);
con.commit();
Log.info(getClass(), "-----Save end-----");
isValidate=true;
result = SUCCESS;
}else{
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}
}else{
if (this.status.equals(SharingConstants.newStatusKey)){
this.setStatus(SharingConstants.drafStatusKey);
if (this.lengthValidate() && this.isSaveValidate(sql)){
entity.setStatus(SharingConstants.drafStatusKey);
//save
sno = sql.save(entity,SharingConstants.saveDocument,SharingConstants.sharing,oldStatus);
Log.info(getClass(), sno + " status:"+this.getStatusName());
Log.info(getClass(), sno + " save finished");
this.setSno(sno);
entity=sql.getSharing(sno,"</p>");
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
con.commit();
if (upload != null)
SharingUtil.getFile(upload, this.getUploadFileName(), sno,
SharingConstants.sharing);
Log.info(getClass(), "-----Save end-----");
isValidate=true;
result = SUCCESS;
}else{
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}
}else{
if (status.equals(SharingConstants.deployStatusKey)){//deploy
if (this.lengthValidate() && isValidate(SharingConstants.Deployed,sql)) {
SharingEntity shEntity=sql.getSharing(sno,"</p>");
entity.setAlertSite(shEntity.getAlertSite());
entity.setAlertDept(shEntity.getAlertDept());
entity.setDeployDept(shEntity.getDeployDept());
entity.setDeploySite(shEntity.getDeploySite());
entity.setDSite(shEntity.getDSite());
entity.setDDept(shEntity.getDDept());
//Start CR-2014-01727 Kevin
entity.setAlertCopy(shEntity.getAlertCopy());
entity.setDeployCopy(shEntity.getDeployCopy());
//End CR-2014-01727 Kevin
sno=sql.save(entity,SharingConstants.saveDocument,SharingConstants.sharing,oldStatus);
this.setSno(sno);
entity=sql.getSharing(sno,"</p>");
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
con.commit();
if (upload != null)
SharingUtil.getFile(upload, this.getUploadFileName(), sno,
SharingConstants.sharing);
Log.info(getClass(), "-----Save end-----");
isValidate=true;
result = SUCCESS;
}else{
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}
}else{
if (this.lengthValidate() && isSaveValidate(sql)) {
sno=sql.save(entity,SharingConstants.saveDocument,SharingConstants.sharing,oldStatus);
this.setSno(sno);
entity=sql.getSharing(sno,"</p>");
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
con.commit();
if (upload != null)
SharingUtil.getFile(upload, this.getUploadFileName(), sno,SharingConstants.sharing);
Log.info(getClass(), "-----Save end-----");
isValidate=true;
result = SUCCESS;
}else{
this.setEntityDesc(sql,sSql);
this.setEntity(entity);
init(sql);
isValidate=false;
result = INPUT;
}
}
}
}
}
if (!tp.equals("Exit")){
isValidate=false;
}
} catch (SizeLimitExceededException e) {
Log.info(this.getClass(), "SizeLimitExceededException:" + e.getMessage());
mess.add("SizeLimitExceededException:"+e.getMessage());
this.setStatusName(SharingConstants.New);
result=INPUT;
if (con!=null) con.close();
} catch (IOException e) {
Log.info(this.getClass(), "IOException:" + e.getMessage());
mess.add("IOException:"+e.getMessage());
this.setStatusName(SharingConstants.New);
result=INPUT;
if (con!=null) con.close();
} catch (FileUploadException e) {
Log.info(this.getClass(), "FileUploadException:" + e.getMessage());
// this.addActionError("Exception:"+e.getMessage());
mess.add("FileUploadException:"+e.getMessage());
this.setStatusName(SharingConstants.New);
result=INPUT;
if (con!=null) con.close();
} catch (Exception e) {
Log.info(this.getClass(), "Save exception:" + e.getMessage());
mess.add("Exception:"+e.getMessage());
this.setStatusName(SharingConstants.New);
result=INPUT;
// this.addActionError("Exception:"+e.getMessage());
if (con!=null) con.close();
}finally{
// this.setEntityDesc(sql,sSql);
// this.setEntity(entity);
//
// init(sql);
// isValidate=false;
if (con!=null) con.close();
}
return result;
}
private String urgency="";
public String geturgency() {
return urgency;
}
public void seturgency(String urgency) {
this.urgency = urgency;
}
}
我认为我应该从 jsp 中获得价值,但没有按照我的预期去做。
注意:我在上面的代码中删除了其他文本字段和下拉菜单,除了我添加的紧急下拉菜单外,所有这些都有效。
在调试模式的控制台上也没有发现异常或错误
您的 urgency
setter 名称不正确;它们应该遵循 JavaBean 命名约定并且是 setUrgency
/getUrgency
.
不相关:
- 操作方法应命名为
save
以遵循 Java 命名约定。 - 所有参数应通过setters(或
ModelDriven
)检索,无需在此处访问操作上下文。 - 您的大部分异常处理都可以通过单个 multi-catch 处理。
- 此操作在单个方法中做得很远,很远太多了:重构。
- 一般来说,setters 应该设置即可。如果 setter 做的不仅仅是设置,它应该有一个更明显的名字。
还有更多,但现在已经足够了:)