Jump to content
  • entries
    27
  • 评论
    5
  • 查看
    67,885

一点struts的小总结~


访客

1,016 查看

这几天忙着上课,太累了~ 明天写作业,陪老婆玩~一般的流程是,首先应该设置好 servlet映射,在web.xml里面

  <!-- Action Servlet Mapping --> <servlet-mapping>    <servlet-name>action</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>
.do被映射为一个action,这样jsp页面里的<form:form action="register.do">一类的path就被struts-config.xml里面的  
  <form-beans>    <form-bean name="registerForm" type="app.RegisterForm" />  </form-beans><action-mappings>      <action path="/register"              type="app.RegisterAction"              name="registerForm" >        <forward name="success" path="/success.html" />        <forward name="failure" path="/failure.html" />    </action>  </action-mappings>
所找到,通过name属性再去寻找form-bean,这样就把action和form-bean联系起来了,form-Bean的name和action的name一样!但是from-bean的类名可以和name属性不一样。只要表单向form-bean传递数据,就会生成一个form-bean的实例,此实例包含表单的所有字段,作为属性。然后action会进行相应处理。form-bean的源码
package app;import org.apache.struts.action.*;public class RegisterForm extends ActionForm {  protected String username;  protected String password1;  protected String password2;  public String getUsername () {return this.username;};  public String getPassword1() {return this.password1;};  public String getPassword2() {return this.password2;};    public void setUsername (String username) {this.username = username;};  public void setPassword1(String password) {this.password1 = password;};  public void setPassword2(String password) {this.password2 = password;};}
struts的心脏--action,逻辑处理的源码实现注意:perform 方法已经过时,应该采用execute()方法。客户自己继承action子类,必须重写execute()方法,因为action类在默认情况下返回 null
package app;import org.apache.struts.action.*;import javax.servlet.http.*;import java.io.*;public class RegisterAction extends Action {  public ActionForward perform (ActionMapping mapping,                                ActionForm form,                                HttpServletRequest req,                                HttpServletResponse res) {// ①Cast the form to the RegisterForm    RegisterForm rf = (RegisterForm) form;    String username = rf.getUsername();    String password1 = rf.getPassword1();    String password2 = rf.getPassword2();// ②Apply business logic    if (password1.equals(password2)) {      try {// ③Return ActionForward for success        UserDirectory.getInstance().setUser(username,password1);        return mapping.findForward("success");      } catch (UserDirectoryException e) {        return mapping.findForward("failure");      }    }// ④Return ActionForward for failure    return mapping.findForward("failure");  }}

本文可以运行的程序见附件~

1 评论


Recommended Comments

struts-config.xml配置有一行,指定语言包在\WEB-INF\classes下的ApplicationResources.properties,其中.properties的后缀名不必写,如不加路径,此文件默认在class目录下

  <!-- ========== Message Resources Definitions =========================== -->  <message-resources    parameter="ApplicationResources"/>

  <!-- ========== Global Forward Definitions ============================== -->  <global-forwards>    <forward name="adminLogin" path="/admin/preLogin.do" />  </global-forwards>
为整个应用系统可以使用的action-forward,而不是一个特定的action。
    <action path="/client/register"            type="org.slstudio.tech.user.action.RegisterAction"            name="clientRegisterForm"            scope="request"            validate="true"            input="/client/error_msg.jsp">      <forward name="success" path="/client/user/register_success.jsp" />      <forward name="failed" path="/client/error_page.jsp" />    </action>

当validate=失败时,转向 input="/client/error_msg.jsp"

Link to comment
访客
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 创建新的...