cool"/>
<forward name="regist" path="/regist.jsp"/>
</global-forwards>
<!--还记得web.xml中后缀为cool的请求吗?它们是转到这里处理的。这里相当于struts的Model部分,Model部分是struts中比较灵活的地方。-->
<action-mappings>
<!--处理regist.cools的请求,使用的FormBean是userForm,既test.UserForm类,当处理过程发生错误时将返回index.jsp-->
<action path="/regist" type="test.RegistAction" name="userForm" scope="request" input="/index.jsp" />
<action path="/overview" forward="/hello.jsp"/>
<action path="/failed" forward="/wuwu.jsp" />
</action-mappings>
</struts-config>
Struts入门(2)
时间:2010-09-28
三、增加一个FormBean,类路径为test.UserForm,以下是这个类的内容:
package test;
import org.apache.struts.action.ActionForm;
public class UserForm extends ActionForm
{
private String name="lpw";//用户名
private String ps="1111";//密码
public UserForm(){}
public void setName(String s) {name=s;}
public String getName() {return name;}
public void setPs(String s) {ps=s;}
public String getPs() {return ps;}
}
四、增加一个Action的子类,类路径为test. RegistAction,以下是这个类的内容:
package test;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import test.UserForm;
public final class RegistAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
UserForm userform = (UserForm) form;
//此处可以调用其他类来执行数据库写入或其他逻辑判断
// 如果UserForm传来的参数name的值为默认的lpw,将forward到failed,
// 该名称将到struts-config.xml的<global-forwards>中寻找映射的url地址
// (可以是绝对路径,也可以是相对路径),对于本例,是转到failed.cool,
// 还记得吗?后缀为cool的请求全部到action-mappings中寻找
// 对应的action处理,最终目录是wuwu.jsp*/
if( "lpw".equals(userform.getName()) )
return (mapping.findForward("failed"));
else
return (mapping.findForward("regist"));
}
}
五、以下所有新增或修改的页面相当于struts的View部分,把首页index.jsp改成:
<%@ page contentType="text/html;charset=GBK" language="java" %>
<%@ page import = "test.*&qu
|