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.*" %> <a href="overview.cool">站点导航</a><br> <form action="regist.cool" method="post"> <!—表单中的域的名称要和UserForm中的参数一样,就可以实现数据自动获取功能,不需要用request.getParameter(“param”);--> 用户:<input type="text" name="name"><br> 密码:<input type="password" name="ps"><br> <input type=submit value="新增用户"> </form> 六、增加hello.jsp,用于站点导航: <h1>site map</h1>The following is content filling by reader 七、增加wuwu.jsp,当没有新用户登陆时,将转到这个页面: |