`
mmdev
  • 浏览: 12920800 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

在飞实习学J2EE之路:2012.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;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics