在飞实习学J2EE之路:12.9.4 Struts的基本流程

实习公司第二天:

        今天开始自己学习struts1,过几天在学习struts2

开始之前需要下载struts-1.2.9-bin.zip里面有用到的jar包和TLD文件

1新创建一个web项目

2添加JAR包和TLD文件

3添加一个名为input.jsp的JSP网页

<form name="test.do" method="post">

<input  type="text" name="str1">

<input type="text" name="str2">

<input type="submit">

<form>

一个FORM表单里添加2个文本框一个提交按钮

4配置struts-config.xml

<form-beans>

<form-bean name="testForm" type="com.demo.struts.forms.TestForm"></form-bean>

</form-beans>

<action-mapping>

<action path="/test" name="testForm" scope="request" type="com.demo.struts.actions.TestAction" input="/input.jsp">

<forward name="success" path="/success.jsp">

<forward name="failure" path="/error.jsp">

</action>

</action-mapping>

当用户单击input.jsp里的提交按钮时,会由ActionServlet接受该请求(ActionServlet在web.xml里一般不用修改),根据struts-config.xml中的"/test"的<action>配置找到表单累

com.demo.forms.TestForm;

5从Requset对象中取得input.jsp页面中输入的表单参数,分别于类com.demo.struts.forms.TestForm中的属性相对应,input.jsp中有几个表单参数,该类中就必须有几个对应的同名属性。

public class TestForm extends ActionForm {

 private static final long serialVersionUID = 1L;
 protected String str1 = null;
 protected String str2 = null;

 public String getStr1() {
  return str1;
 }
 public void setStr1(String str1) {
  this.str1 = str1;
 }
 public String getStr2() {
  return str2;
 }
 public void setStr2(String str2) {
  this.str2 = str2;
 }
}

6编写Action处理类(重)

public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) thros Exception

{

   ActionErrors errors=new ActionError();

  ActionForward forward=new ActionForward();

   TestForm testForm=(TestForm)form;

   try{

String str1=testForm.getStr1();

String str2=testForm.getStr2();

 

}cath{

}

if(!error.isEmpty())

{

saveError(request,response);

forward=mapping.findForward("failure");

}

else

{

forward=mapping.findForward("success");

 

}

return forward;

}

 

 

资源地址:http://download.csdn.net/detail/zaifeishandong/4547411(自己看看吧,别忘了自己填上JAR包,在lib文件夹里面)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值