web.xml进行如下配置
4、新建登陆页面login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <% String basePath = request.getContextPath(); %> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆页面</title> </head> <body> <h1>登陆页面</h1> <hr> <form action="<%=basePath %>/login.do" method="post" > userName:<input id="userName" name="userName" type="text" /><br> passWord:<input id="passWord" name="passWord" type="password" /><br> <input type="submit" id="submit" name="submit" value="submit" /> </form> </body> </html>
5、新建登陆成功后的跳转页面loginSucces.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆成功页面</title> </head> <body> <h1>欢迎[<%=request.getAttribute("userName") %>]登陆成功!</h1> </body> </html>
6、新建登陆失败后的跳转页面loginError.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆失败页面</title> </head> <body> <h1>登陆失败!</h1> </body> </html>
7、新建LoginActionForm.java
package com.lanp.webapp.form; import org.apache.struts.action.ActionForm; /** * 封装登陆表单数据的FORM类 * @author LanP * @since 2012年4月11日23:07:09 * @version v1.0 */ @SuppressWarnings("serial") public class LoginActionForm extends ActionForm { private String userName; private String passWord; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
8、新建LoginAction.java
package com.lanp.webapp.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.lanp.webapp.form.LoginActionForm; /** * 处理登陆的Action类 * @author LanP * @since 2012年4月11日23:07:09 * @version v1.0 */ public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = "error"; LoginActionForm loginActionForm = (LoginActionForm)form; String userName = loginActionForm.getUserName(); String passWord = loginActionForm.getPassWord(); if(null != userName && "admin".equals(userName) && null != passWord && "admin".equals(passWord)) { path = "success"; request.setAttribute("userName", userName); } else { path = "error"; } return mapping.findForward(path); } }
9、配置struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="loginActionForm" type="com.lanp.webapp.form.LoginActionForm"> </form-bean> </form-beans> <action-mappings> <action path="/login" type="com.lanp.webapp.action.LoginAction" name="loginActionForm" scope="request"> <forward name="success" path="/jsp/loginSucces.jsp" /> <forward name="error" path="/jsp/loginError.jsp" /> </action> </action-mappings> </struts-config>
OK,TKS!