七、整合struts
1.在WEB-INF目录下新建login.jsp并编辑处理
1.1编辑login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% 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 'login.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> <h1>管理员登陆</h1> <!-- 准备交给path为/login的分派Action处理,里面的login函数处理登录请求 --> <form action="/essh/login.do?flag=login" method="post"> <table> <tr><td>id:</td><td><input type="text" name="id"></td></tr> <tr><td>密码:</td><td><input type="password" name="password"></td></tr> <tr><td><input type="submit" value="登录"></td><td><input type="reset" value="重置"></td></tr> </table> </form> </body> </html>
1.2在index.jsp中,将首页面跳往此界面
<body> <jsp:forward page="/WEB-INF/login.jsp"></jsp:forward> </body>
2.建立编写struts中的action和form,配置struts-config.xml文件
2.1建包com.myz.web.actions,在包下新建LoginAction
package com.myz.web.actions; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; public class LoginAction extends DispatchAction { public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub return super.execute(mapping, form, request, response); } }
2.2建包com.myz.web.forms
import org.apache.struts.action.ActionForm; public class EmployeeForm extends ActionForm { //属性名称与login.jsp中的控件名称一样 private Integer id; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2.3在WEB-INF下新建并配置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="employeeForm" type="com.myz.web.forms.EmployeeForm"></form-bean> </form-beans> <action-mappings> <action path="/login" parameter="flag" name="employeeForm" type="com.myz.web.actions.LoginAction"> <forward name="ok" path="/WEB-INF/mainFrame.jsp"></forward> <forward name="err" path="/WEB-INF/login.jsp"></forward> </action> </action-mappings> </struts-config>
2.4依据配置信息,在WEB-INF下新建mainFrame.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% 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 'mainFrame.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> <h2>欢迎登录</h2> <a href="#">添加员工</a><br/> <a href="#">显示员工</a><br/> <a href="#">删除员工</a><br/> <a href="#">修改员工</a><br/> </body> </html>
2.5对LoginAction中的login函数修改,使其能够对提交的信息进行简单处理
public class LoginAction extends DispatchAction { public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub EmployeeForm employeeForm=(EmployeeForm) form; //简单验证,如果密码是123就通过 if(employeeForm.getPassword().equals("123")){ return mapping.findForward("ok"); }else{ return mapping.findForward("err"); } } }
2.6在web.xml中配置ActionServlet总控制器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>struts</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> </servlet> <servlet-mapping> <servlet-name>struts</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.测试,将项目发布到tomcat,浏览器中输入地址http://localhost:8080/essh
3.1输入用户名和密码,输入密码为123则登录到mainFrame.jsp主页面
3.2只要密码不为123,则返回登录界面,登录失败