本实例是Struts1的一个入门实例
采用的编辑器是Myeclipse 10
一,创建模型
本事列涉及两个JavaBean
1.UserInfo.java:描述用户的实体类
package cx.mystruts.entity; public class UserInfo { private String uid; private String pwd; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public UserInfo(String uid, String pwd) { super(); this.uid = uid; this.pwd = pwd; } public UserInfo() {} }
2.UserService.java:处理业务逻辑的JavaBean
package cx.mystruts.service; import java.util.HashMap; import java.util.Map; import cx.mystruts.entity.UserInfo; public class UserService { //构造Map对象,用于存入UserInfo的集合 private Map userInfoMap=new HashMap(); //构造UserService静态对象,实现单列工厂模式 private static UserService userService=new UserService(); //构造函数私有,单列模式,模拟数据库产生UserInfo的集合 private UserService(){ try{ UserInfo u=new UserInfo("cx","123456"); userInfoMap.put(u.getUid(), u); } catch(Exception e){ e.printStackTrace(); } } //定义静态方法 public static UserService newInstance(){ return userService; } public UserInfo login(String uid,String pwd){ //判断用户输入的用户名是否在集合中 if(this.userInfoMap.containsKey(uid)){ UserInfo findUserInfo=(UserInfo)this.userInfoMap.get(uid); if(findUserInfo.getPwd().equals(pwd)){ return findUserInfo; } return null; } return null; } }
二.以下步骤需要添加Struts支持,直接使用MyEclipse添加,这里不介绍
1.创建ActionForm
package cx.struts; import org.apache.struts.action.ActionForm; public class LoginActionForm extends ActionForm { private String uid; private String pwd; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
2.创建控制器
(1)ActionMapping:包含Action的配置信息,与Struts-config.xml配置文件中的<action>元素对应。
(2)ActionForm:包含用户的表单数据。当Struts1框架调用execute()方法时,ActionFrom中的数据已经通过了表单验证。
(3)HttpServletRequest:当前的HTTP请求对象
(4)HttpServletResponse:当前的HTTP响应对象
package cx.struts; 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 cx.mystruts.entity.UserInfo; import cx.mystruts.service.UserService; public class LoginAction extends Action{ @Override public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { LoginActionForm frm=(LoginActionForm)form; String uid=frm.getUid(); String pwd=frm.getPwd(); UserInfo user=UserService.newInstance().login(uid, pwd); if(user!=null){ request.setAttribute("user", user); return mapping.findForward("success"); } return mapping.findForward("fail"); } }
三.视图
1.登陆界面index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>User Login</title> </head> <body> <form action="login.do" method="post"> UID:<input type="text" name="uid" /> PWD:<input type="text" name="pwd"/> <input type="submit" value="ok"> </form> </body> </html>
2.登陆成功界面success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Success</title> </head> <body> <h1>Login Success</h1> UID:${user.uid }<br/> PWD:${user.pwd } </body> </html>
3.登陆失败界面fail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>fail</title> </head> <body> <h1>Login Fail</h1> </body> </html>
四.Struts配置文件
1.webxml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name /> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2.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> <data-sources /> <form-beans> <form-bean name="loginActionForm" type="cx.struts.LoginActionForm"/> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <action name="loginActionForm" path="/login" type="cx.struts.LoginAction"> <!-- 配置Action中的success转发,对应于index.jsp页面 --> <forward name="success" path="/success.jsp"/> <!-- 配置Action中的fail转发,对应于error.jsp页面 --> <forward name="fail" path="/fail.jsp"/> </action> </action-mappings> <message-resources parameter="cx.struts.ApplicationResources" /> </struts-config>
OK!到此,整个登陆流程就Ok!