struts

1.ActionServlet作为servlet,通过struts-config.xml来分配处理请求,ActionMapping,ActionForm和Action
2.访问某个jsp页面,在struts-config.xml中设置了关联该Action的Action类和ActionForm类.其中ActionForm其实类似于一直JavaBean,把
页面上的请求内容转化为JavaBean,在Action类的perform(ActionMapping
mapping,ActionForm form,HttpServletRequest req,HttpServletResponse
res)中来实现
业务逻辑,并且通过mapping.findForward("XXX")来跳转页面
完整的例子如下:
register.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html locale="true">
<head>
<title>RegUser</title>
<html:base />
</head>
<body bgcolor="white">
<html:errors />
<html:form action="/regUserAction" focus="logname">
<table border="0" width="100%">
<tr>
<th align="right">
Logname:
</th>
<td align="left">
<html:text property="logname" size="20" maxlength="20" />
</td>
</tr>
<tr>
<th align="right">
Password:
</th>
<td align="left">
<html:password property="password" size="20" maxlength="20" />
</td>
</tr>
<tr>
<th align="right">
E-mail:
</th>
<td align="left">
<html:password property="email" size="30" maxlength="50" />
</td>
</tr>
<tr>
<td align="right">
<html:submit property="submit" value="Submit" />
</td>
<td align="left">
<html:reset />
</td>
</tr>
</table>
</html:form>
</body>
</html:html>

strtus-config.xml:
<struts-config>
<form-beans>
<form-bean name="regUserForm"
type="com.datangmobile.RegUserForm" />
</form-beans>
<action-mappings>
<action path="/regUserAction"
type="com.datangmobile.RegUserAction"
name="regUserForm"
attribute="regUserForm"
scope="request" validate="false">
<forward name="failure" path="/messageFailure.jsp" />
<forward name="success" path="/messageSuccess.jsp" />

</action>
</action-mappings>
</struts-config>

RegUserForm.java
package com.datangmobile;
public class RegUserForm extends ActionForm {
private static final long serialVersionUID = -6439072301208779400L;
private String logname;
private String password;
private String email;
public RegUserForm() {
logname = null;
password = null;
email = null;
}
public String getLogname() {
return this.logname;
}
public void setLogname(String logname) {
this.logname = logname;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return this.password;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail(){
return this.email;
}
public void reset(ActionMapping mapping,HttpServletRequest request){
logname=null;
password=null;
email=null;
}
}

RegUserAction.java
package com.datangmobile;
public final class RegUserAction extends Action {
public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) {
String title = req.getParameter("title");
String password = req.getParameter("password");
String email = req.getParameter("email");
/*
* 取得用户请求,做相应数据库操作,略
*/
return mapping.findForward("success");
}
}

ActionMapping.findForward(…)方法既从它的本地范围又从全局范围提供一个ActionForward对象,该对象返回至RequestProcessor以RequestDispatcher.forward(…)或response.sendRedirect(…)调用下一个视图。当<forward>元素有redirect="false"属性或redirect属性不存在的时候,RequestDispatcher.forward(…)被执行;当redirect="true"是,将调用sendRedirect(…)方法。下例举例说明了redirect属性的用法:
  <forward name="success" path="/Catalog.jsp" redirect="true"/>
如果redirect=true,
URL建立如/contextPath/path因为HttpServletResponse.sendRedirect(…)中解释URL采用"/"开头相对于servlet容器根目录。
如果redirect=false,
URI建立如/path因为ServletContext.getRequestDisptacher(…)采用虚拟目录相关URL。

直接从ActionFrom类继承的reset()和validate()方法,并不能实现什么处理功能,所以有必要自己重新覆盖。

在struts1.1中,新增了validation
framework。增加了对form数据提交的验证。将原本需要在ActionFrom
Bean的validate()进行的验证通过配置文件的描述进行验证。
有关其详细信息,请参考 http://home.earthlink.net/~dwinterfeldt
。个人建议对于小型应用系统可以采用这种配置方式,但是对于应用系统中有大量web层表单应用的系统,并且业务需求变动比较大的,使用validation
framework 可能会加重开发难度、系统维护难度。可以借鉴validation
framework的Javascript Validator Tag。

struts提供了一组可扩展的自定义标签库(TagLib),可以简化创建用户界面的过程。目前包括:Bean
Tags,HTML Tags,Logic Tags,Nested Tags,Template Tags 这几个Taglib
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值