<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<!-- 对FormBean的定义 -->
<form-beans>
<!-- FormBean的名称和类型,这里是动态FromBean -->
<form-bean name="loginForm"
type="org.apache.struts.action.DynaActionForm">
<!-- FormBean中的属性 -->
<form-property name="username" type="java.lang.String" />
<form-property name="password" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<!-- 以下为两个全局的forward转向 -->
<global-forwards>
<forward name="success" path="/form/main.jsp" redirect="true" />
<forward name="fail" path="/form/fail.jsp" redirect="true" />
</global-forwards>
<!-- action的定义 -->
<action-mappings>
<!-- attribute:属性 input:对应输入的JSP文件 name:Action的名称
path:Action的访问路径 scope:Action的作用范围 type:Action类的全名 -->
<action attribute="loginForm" input="login.jsp" name="loginForm"
path="/login" scope="request"
type="com.meixin.struts.action.LoginAction" />
</action-mappings>
<message-resources
parameter="com.meixin.struts.ApplicationResources" />
</struts-config>
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 org.apache.struts.action.DynaActionForm;
public class LoginAction extends Action
{
String username = "";
String password = "";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
// 通过loginForm可以自动地得到用户名和密码
DynaActionForm loginForm = (DynaActionForm) form;
// 获取用户名,返回Object类型,需要强制转换
username = (String) loginForm.get("username");
// 获取密码
password = (String) loginForm.get("password");
if (username.equals("meixin") && password.equals("123456"))
{
// 若合法则转向success对应的页面
return mapping.findForward("success");
}
else
{
// 若不合法,则转向fail所对应的页面
return mapping.findForward("fail");
}
}
}
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for DynaActionForm form</title>
</head>
<body>
<!-- 这里要输出处理表单的action访问路径 -->
<html:form action="/login.do">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'main.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
${username },欢迎您!<br>
</body>
</html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'fail.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
对不起,你输入的用户名和密码不正确!<a href="form/login.jsp">请重新输出!</a><br>
</body>
</html>