2008年8月9日

struts-config.xml中 action 的attribute属性与name属性

1、在一般情况下,actionForm是被存储在一定的scope中(request或session,通过action的scope属性来配置),当我们在配置时,指定name而不指定attribute,那么指定的name值就作为actionForm存储在scope中的key值,我们可以在action中通过httpServletRequest.getAttribute("指定的name属性值")来获得这个actionForm;     当我们既配置了name又配置了attribute,那么actionForm存储在scope中的key值就采用attribute属性指定的值了,这时要通过httpServletRequest.getAttribute("指定的attribute属性值")来获得actionForm,此时通过httpServletRequest.getAttribute("指定的name属性值")是不能获得actionForm的。  
   
    所以,是否配置attribute属性就决定了actionForm存储在scope中的key值是采用name,还是采用attribute

 2、 在《Programming Jakarta Struts》这本书中的第四章“Configuring the Struts Application”中这样一段说明来分别阐述这两
个属性:(102页)
++++++++
atribute:
++++++++
The name of the request or session scope attribute under which the form bean for this action can be accessed.
A value is only allowed here if there is a form bean specified in the name attribute. This attribute is
optional and has no default value.

++++++++
name:
++++++++
The name of the form bean, if any, that is associated with this action. This value must be the name attribute
from one of the form-bean elements defined earlier. This attribute is optional and has no default value.

最初看这些真的还是不好区分这两者。不过在仔细看过struts的源代码以后,豁然开朗。。。

下面主要对attribute进行解释,应为没有人会对name属性不了解的(呵呵。。。)


解释:在struts实例化actionform的时候,有两种情况:如果已经存在,那么从内存中取回;如果第一次实例化,那么创建,并放入内存。
这样就有一个问题了,struts是根据什么来取回并创建actionform的呢,答案就是attribute的值。让我们进入struts的源代码:

/**
*创建或者取回formbean方法
*该方法在:org.apache.struts.util.RequestUtils中
*/
public static Actionform createActionform(
HttpServletRequest request,
ActionMapping mapping,
ModuleConfig moduleConfig,
ActionServlet servlet) {
。。。。
。。。
// Is there a form bean associated with this mapping?
//得到action mapping中attribute的值
String attribute = mapping.getAttribute();
。。。。
。。。。
Actionform instance = null;
HttpSession session = null;
//yes!!就在这里了,把创建以后的actionform放在request或者session里,看到放入的名字了么,就是mapping.getAttribute();
if ("request".equals(mapping.getScope())) {
instance = (Actionform) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (Actionform) session.getAttribute(attribute);
}
。。。
。。。


}


下面又有一个问题浮出水面:如果我没有在action mapping中指定attribute呢,那struts 是如何解决的?
答案很简单,如果单从结果上看,此时struts使用的name的值,为什么呢,看struts源代码:

/**
* The request-scope or session-scope attribute name under which our
* form bean is accessed, if it is different from the form bean's
* specified <code>name</code>.
*该代码在:org.apache.struts.config.ActionConfig中
*/
protected String attribute = null;

public String getAttribute() {
//yes!!!!就在这里,看到了吧,如果你没有设定attribute,那么struts 会把name的值拿过来用。呵呵。。。
if (this.attribute == null) {
return (this.name);
} else {
return (this.attribute);
}
}

public void setAttribute(String attribute) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.attribute = attribute;
}

posted @ 2008-09-20 13:37 chenkai 阅读(683) | 评论 (1)编辑 收藏

Request的getParameter和getAttribute方法的区别

当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getAttribute方法来取得要共享的数据。而MVC中用的就是Web组件之间的转发啊!真是笨,怎么当时没有想到呢?

      下面整理一下getParameter和getAttribute的区别和各自的使用范围。

      (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法

      (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:

      <a  href="authenticate.jsp?username=wolf">authenticate.jsp  </a>

      或者:

      <form  name="form1"  method="post"  action="authenticate.jsp">
          请输入用户姓名:<input  type="text"  name="username">
          <input  type="submit"  name="Submit"  value="提交">
      </form>

       在authenticate.jsp中通过request.getParameter("username")方法来获得请求参数username:

       <%  String  username=request.getParameter("username");  %>

       (3)当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。

        假定  authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字,  如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:

        <%
        String  username=request.getParameter("username");
        request.setAttribute("username",username);
        %>

        <jsp:forward  page="hello.jsp"  />

        在hello.jsp中通过getAttribute()方法获得用户名字:

        <%  String  username=(String)request.getAttribute("username");  %>
        Hello:  <%=username  %>

        从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。

        request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据。

        request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据。

        request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段。

        getAttribute是返回对象,getParameter返回字符串

        总的来说:request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。

posted @ 2008-09-20 09:57 chenkai 阅读(2777) | 评论 (0)编辑 收藏

用javascript实现table的排序

<%@page
language="java"
contentType="text/html;charset=GBK"
%>

<html>
<head>
    <title>MyHtml.html</title>
    <meta http-equiv="content-type" content="text/html; charset=gbk">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

   <script  language="javascript">

 

function   JM_PowerList(colNum)  
  {  
  headEventObject=event.srcElement;//取得引发事件的对象  
  while(headEventObject.tagName!="TR")   //不是tr行,则从底下的td冒泡上来寻找到相应行  
  {  
  headEventObject=headEventObject.parentElement;  
  }  
   
  for   (i=0;i<headEventObject.children.length;i++)  
  {   alert(headEventObject.children[i].tagName);
  if   (headEventObject.children[i]!=event.srcElement)//找到事件发生的td单元格  
  {  
  headEventObject.children[i].className='listTableHead';//把点击的列的className属性设为listTableHead  
  }  
  }  
   
  var   tableRows=0;  
  trObject=clearStart.children[0].children;   //取得表格中行对象,   原来这里叫DataTable,   可能是你写错了吧??  
  for   (i=0;i<trObject.length;i++)  
  {  
  Object=clearStart.children[0].children[i];//取得每行的对象  
  tableRows=(trObject[i].id=='ignore')?tableRows:tableRows+1;//如果不是忽略行,则行数加一  
  }  
   
  var   trinnerHTML=new   Array(tableRows);    
  var   tdinnerHTML=new   Array(tableRows);  
  var   tdNumber=new   Array(tableRows)  
  var   i0=0  
  var   i1=0  
  for   (i=0;i<trObject.length;i++)  
  {  
  if   (trObject[i].id!='ignore')  
  {  
  trinnerHTML[i0]=trObject[i].innerHTML;//把行放在数组里  
  tdinnerHTML[i0]=trObject[i].children[colNum].innerHTML;//把要排序的行中td的内容放数组里  
  tdNumber[i0]=i;//行号  
  i0++;//加一,下个循环用  
  }  
  }  
  sourceHTML=clearStart.children[0].outerHTML;//取得表格中所有tr的html代码  
   
  //对所有td中的字符串进行排序,   算不算冒泡排序???  
  for   (bi=0;bi<tableRows;bi++)  
  {  
  for   (i=0;i<tableRows;i++)  
  {  
  if(tdinnerHTML[i]>tdinnerHTML[i+1])  
  {  
  t_s=tdNumber[i+1];  
  t_b=tdNumber[i];  
  tdNumber[i+1]=t_b;  
  tdNumber[i]=t_s;  
  temp_small=tdinnerHTML[i+1];  
  temp_big=tdinnerHTML[i];  
  tdinnerHTML[i+1]=temp_big;  
  tdinnerHTML[i]=temp_small;  
  }  
  }  
  }  
   
   
   
  var   showshow='';  
  var   numshow='';  
  for   (i=0;i<tableRows;i++)  
  {  
  showshow=showshow+tdinnerHTML[i]+' ';//把排序好的td的内容存在showshow字串里  
  numshow=numshow+tdNumber[i]+'|';             //把排序好的相应的行号也存在numshow中  
  }  
   
  sourceHTML_head=sourceHTML.split("<TBODY>");//从<TBODY>截断,我试了,前头串为空  
   
  numshow=numshow.split("|");  
  var   trRebuildHTML='';  
  if   (event.srcElement.className=='listHeadClicked')  
  {//已点击的列,   则逆排  
  for   (i=0;i<tableRows;i++)  
  {  
  trRebuildHTML=trRebuildHTML+trObject[numshow[tableRows-1-i]].outerHTML;//取出排序好的tr的内容连接起来  
   
  }  
  event.srcElement.className='listHeadClicked0';  
  }  
  else  
  {//默认顺排,新点击顺排  
  for   (i=0;i<tableRows;i++)  
  {  
  trRebuildHTML=trRebuildHTML+trObject[numshow[i]].outerHTML;  
  }  
  event.srcElement.className='listHeadClicked';  
  }  
  //取得排序后的tr集合结果字符串  
  var   DataRebuildTable='';  
  //把旧的表格头和新的tr排序好的元素连接起来,   (修改了一下)  
  DataRebuildTable   =   "<table   border=1 width=100%  cellpadding=1 cellspacing=1 id='clearStart'><TBODY>"
      +   trObject[0].outerHTML   +   trRebuildHTML   +   "</TBODY>"   +      "</table>";  
  clearStart.outerHTML=DataRebuildTable;//表格用新串重新写一次  
   
  }  

</script>

  </head>
<table border=1 id="clearStart">
 <tr bgcolor=cccccc id='ignore'>
  <td onclick="JM_PowerList(0)">列一
  </td>
  <td onclick="JM_PowerList(1)">
   列二
  </td>
  <td onclick="JM_PowerList(2)">
   列二
  </td>
 </tr>
  <tr>
  <td>
   周
  </td>
  <td>
   公务员
  </td>
  <td>
   22
  </td>
 </tr>
 <tr>
  <td>
   张三
  </td>
  <td>
   研究员
  </td>
  <td>
   65
  </td>
 </tr>
 <tr>
  <td>
   李思
  </td>
  <td>
   科学家
  </td>
  <td>
   24
  </td>
 </tr>
 <tr>
  <td>
  王武
  </td>
  <td>
   社会学家
  </td>
  <td>
   38
  </td>
 </tr>
</table>
</body></html>

 

posted @ 2008-08-09 11:40 chenkai 阅读(386) | 评论 (0)编辑 收藏

<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(2)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜