一点struts的小总结~
这几天忙着上课,太累了~ 明天写作业,陪老婆玩~一般的流程是,首先应该设置好 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
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new account登录
Already have an account? Sign in here.
现在登录