<li>空字段测试</li><br>
   <form action="blankfield.do" method="post">
    姓名:<input type="text" name="username"><br>
    <input type="submit" value="提交">
   </form>

建立一个BlankFieldActionForm接收空字段

package com.bjsxt.struts;

import org.apache.struts.action.ActionForm;

/**
 * 测试空字段
 * @author Administrator
 *
 */
public class BlankFieldActionForm extends ActionForm {

 private String username;
 
 private String password;

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }
}

建立Action:

package com.bjsxt.struts;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * 测试空字段
 * @author Administrator
 *
 */
public class BlankFieldTestAction extends Action {

 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  return mapping.findForward("success");
 }

}

配置:

<action path="/blankfield"
    type="com.bjsxt.struts.BlankFieldTestAction"
    name="blankFieldForm"
    scope="request"  
  >
   <forward name="success" path="/blankfield_success.jsp"/>
  </action>

jsp测试页面:

<body>
 <%
  BlankFieldActionForm form = (BlankFieldActionForm)request.getAttribute("blankFieldForm");
  %>
 <li>jsp脚本</li><br>
 姓名:<%=form.getUsername() %><br>
 密码:<%=form.getPassword() %><br>
 <p>
 <li>el表达式</li><br>
 姓名:${blankFieldForm.username }<br>
 密码:${blankFieldForm.password }<br>
</body>

在空字段中jsp脚本得到的是null空对象,而el表达式得到的是一个空字符串。

posted on 2011-02-13 22:06  魔战  阅读(300)  评论(0编辑  收藏  举报