浏览 126 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-05-20 关键字: struts
6.2. 演示/部署示例(用户登录)功能
演示用户登录功能示例 将用户登录war文件部署到自己的tomcat中 6.3. 编写用户登录的步骤 6.3.1. 第一步: 编写logon.jsp 切换行号显示切换行号显示 <?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>登录</title>
</head>
<body>
<form action="logon.do" method="post">用户名: <input type="text"
name="userName" /><br />
密码:<input type="password" name=password" /><br />
<input type="submit" name="Submit" value="登录" /></form>
</body>
</html>
6.3.2. 第二步: 编写登录成功页面success.jsp 切换行号显示切换行号显示 <?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>登录成功</title>
</head>
<body>
${param.userName }登录成功.
</body>
</html>6.3.3. 第三步: 编写FormBean
切换行号显示切换行号显示 package struts.logon;
import org.apache.struts.action.ActionForm;
public class LogonFormBean extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
private String userName;
private String password;
//省略getter和setter
}
6.3.4. 第四步: struts-config.xml中配置FormBean
...
<form-beans>
<form-bean name="logonForm" type="struts.logon.LogonFormBean" />
</form-beans>
...
6.3.5. 第五步: 编写控制类的代码 用于实现页面的跳转控制 继承org.apache.struts.action.Action 覆盖execute方法: 切换行号显示切换行号显示 @Override
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
LogonFormBean bean = (LogonFormBean) actionForm;
if (bean.getUserName().equals("test")) {
return actionMapping.findForward("success");
}
return actionMapping.findForward("error");
}
}
方法的逻辑: 如果用户名等于"test", 跳转到成功, 否则返回登录
6.3.6. 第六步: 配置控制功能 控制功能负责页面的跳转 如果用户名等于"test", 跳转到成功页面, 否则返回登录页面 在struts-config.xml文件中加入: <action-mappings>
<action path="/logon" type="struts.logon.LogonAction" name="logonForm">
<forward name="success" path="/success.jsp" />
<forward name="error" path="/logon.jsp" />
</action>
</action-mappings>
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |


