struts 复习1(速动画)

本文介绍了一个基于Struts框架实现的简单HelloWorld示例,包括ActionForm类的定义、验证逻辑及JSP页面展示等内容,展示了Struts框架的基本使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 *.properties 配置 jsp.hello.title=hello world!!!! jsp.hello.error=This is error!
ActionForm

 

package com.yourcompany.struts.form; 
import javax.servlet.http.HttpServletRequest; 
import org.apache.struts.action.ActionError; 
import org.apache.struts.action.ActionErrors;
 import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionMapping;
 public class HelloForm extends ActionForm 
{ 
private String userName;

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
 {
   ActionErrors errors=new ActionErrors();  
   if((userName==null)||(userName.length()<1))  
   {   
   errors.add("userName",new ActionError("jsp.hello.error"));   
    }  
  return errors; 
  }

  public void reset(ActionMapping mapping, HttpServletRequest request) 
 {   
  this.userName=null;    
  } 
  public String getUserName() 
  { 
   return userName; 
  } 
  public void setUserName(String userName)
  { 
  this.userName = userName;  
  }
 }


 

 

Action就是加了一个mapping package com.yourcompany.struts.action; 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; import com.yourcompany.struts.form.HelloForm; public class HelloAction extends Action {

 public ActionForward execute(ActionMapping mapping, ActionForm

form,    HttpServletRequest request,

HttpServletResponse response) {   HelloForm helloForm = (HelloForm) form;// TODO Auto-

generated method stub   return mapping.findForward("Hello");  } }

jsp <%@ page language="java" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"

prefix="bean"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>   <html>  <head>   <title>JSP for HelloForm form</title>  </head>  <body>   <html:form action="/hello">    userName : <html:text

property="userName"/><html:errors property="userName"/><br/>    <html:submit/><html:cancel/>   </html:form>  </body> </html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值