6.struts登陆页面的演示

  1.创建一个web project "Struts_1"    
    添加struts的jar包 --在项目文件右键->myeclipse->add struts...
        上面的步骤已经将web.xml中加入了struts的映射
  2.在src文文件下建2个包com.amaker.action,->LoginAction.java;com.amaker.form->LoginForm.java
    
//LoginAction.java
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.amaker.form.LoginForm;
public class LoginAction extends Action{
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  System.out.println("mapping.....");
//	 String username=request.getParameter("username");
//	 String password=request.getParameter("password");
  //上面2行需要表单的处理
  LoginForm login=(LoginForm)form;
  String username=login.getUsername();
  String password=login.getPassword();
 
  if(username!=null&&username.equals("lily"))
  {
//	 request.getRequestDispatcher("/Success.html").forward(request, response);
   return mapping.findForward("success");
  }else{
//	 request.getRequestDispatcher("/Failure.html").forward(request, response);
   return mapping.findForward("failure");
  }
  //上面的return需要调转页面的处理
//	 return null;
 }
}

  

//LoginForm.java
package com.amaker.form;
import org.apache.struts.action.ActionForm;
public class LoginForm 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;
 }
}

  3.配置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="loginForm" type="com.amaker.form.LoginForm"></form-bean> 
</form-beans> 
//<form-beans>在.xml位置不能变不然会报错 //表格名字要与<action path="/login" //type="com.amaker.action.LoginAction" name="loginForm"> 相同 <global-exceptions /> <global-forwards /> <action-mappings> <action path="/login" type="com.amaker.action.LoginAction" name="loginForm"> <forward name="success" path="/Success.html"></forward> <forward name="failure" path="/Failure.html"></forward> </action> </action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" /> </struts-config>

  4.index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'index.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>
    <form name="f1" id="f1" action="<%=path%>/login.do" method="post">
      <table border="0">
        <tr>
          <td>Username:</td>
          <td><input type="text" name="username" ></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="password" id="password"></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><input type="submit" value="Login"></td>
        </tr>
      </table>
    </form>
  </body>
</html>

  5.Failure.html  

<!DOCTYPE html>
<html>
  <head>
    <title>MyHtml.html</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 
  <body>

  faliure!.....
  </body>
</html>

  6.Success.html

<!DOCTYPE html>
<html>
  <head>
    <title>MyHtml.html</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 
  <body>
  Success!........
  </body>
</html>

  


总结:
表单与Java类通过xml文件配置联系在一起,使使用时的类的名字更好记,而且易于改动
------------------------------------------------------------------------------------------------------------------------------本娃的学习日记@lily园
原文地址:https://www.cnblogs.com/yanglicyfsdm/p/j2ee.html