struts2.0-注意事项1则-action中setter-getter方法与bean class文件的getter-setter不同!!

来源:互联网 发布:淘宝交易几笔达到3心 编辑:程序博客网 时间:2024/05/25 19:58

 <s:form  action="myAction" method="post">
  <label></label>
  <table width="100%" border="0" cellspacing="1" cellpadding="0">
    <tr>
      <td colspan="2"> 添加</td>
      <td width="6%">&nbsp;</td>
    </tr>
    <tr>
      <td width="13%">&nbsp;</td>
      <td width="81%">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><div align="right"></div></td>
      <td><label>
        <s:textfield  name="RName" label="名称" size="60"/>
      </label></td>
      <td>&nbsp;</td>
.......

=====struts.xml文件

 <action name="myAction" 
   class="com.Struts.Action.myAction" method="Save">
   <result name="saveSuccess">/xx/Add.jsp</result>
  </action> 

======

package com.Struts.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

...
import com.Service.HibernateSessionService;
//import com.Struts.Action.BaseAction;
import com.opensymphony.xwork2.ActionSupport;


public class myAction extends ActionSupport {

 private String RName;
 
 public String getRName(){  //注意 get为小写,不能写成Get,后面的RName一个字母也不能少,也不能大小写不一
  return RName;
 }
    public void setRName(String RName){  //同样 set为小写.不能写成Set,这一点与Bean class文件的Set方法有区 别
     this.RName=RName;
    }
 
 public String Save() throws Exception {
  
   ...
  ....     
        try
     {
      Hi.TransactionBegin();
        
         RAllRes RmyModel=new Rmy();
         
         RmyModel.setRType("xxxxx");
         RmyModel.setRTypeId(123);
         RmyModel.setRName(RName); //  否则此处RName取不到值!
         myService.AllResSave(RmyModel);

         
               Hi.TransactionCommit();
     }catch (Exception ee )
     {
      Hi.TransactionRollback();
   ee.printStackTrace();
     }
     return "saveSuccess";
 }

...
 }
 

}

 

===============

Set----->set

Get----->get

否则,就取不回form中的数值!

原创粉丝点击