[转载]关于struts的学习!

来源:互联网 发布:依云订水软件 编辑:程序博客网 时间:2024/04/30 09:52

主要就是FormBean,Action还有ActionMapping的Forward搞清楚就是了 ,
在struts-cofig.xml文件中配置FormBean和Action:
  <form-beans>
  <form-bean name="book" type="com.xujun.BookForm">
//如果想添加formBean中没有的属性,可以这样自定义一个属性
   <form-property name="srvName" type="java.lang.String"/>
  </form-bean>
  </form-beans>
 
 <action-mappings>
//name 是指formbean的Name,path必须和jsp中的form的提交的action的path一样,type是
  Action对应的class
    <action  name="book" path="/action" type="com.xujun.ShowBookAction"    
  validate="true">
    <forward name="returnIndex" path="/index.jsp"/>
    </action>
  </action-mappings>

jsp页面:
<%@page contentType="text/html; charset=gb2312"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html:html>
<body>
  <html:form action="/action" method="POST">
    <a>作者</a>
    <html:text property="author" name="book"/>
    <a>价格</a>
    <!--<html:text property="price" name="book"/>-->
    <html:select property="price" name="book">
      <html:option value="100">      </html:option>
      <html:option value="200">      </html:option>
    </html:select>
    <a>书名</a>
   <html:text property="bookName" name="book"/>
    <input type="reset" value="重 填">
    <input type="submit" value="提 交">
    <input type="button" value="返 回" onClick="javascript:history.back();">
  </html:form>
</body>
</html:html>
Aciont:
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
public class ShowBookAction extends Action {
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest servletRequest,
                                 HttpServletResponse servletResponse) {
        BookForm bookForm = (BookForm) actionForm;
        double price = bookForm.getPrice();
        System.out.println("" + price + "   " + bookForm.getAuthor() + "  " +
                           bookForm.getBookName());
        return actionMapping.findForward("returnIndex");
    }
}
FormBean可以用jbulider中的FormBean自动完成,struts-config可以用IDE的可视环境编辑。
用jbulider开发struts,非常方便和简单,jbuilder作的真是强大啊

原创粉丝点击