struts 登录实例

来源:互联网 发布:淘宝ceo和天猫ceo 编辑:程序博客网 时间:2024/05/16 11:56

web.xml 文件

 

<?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">
  <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>
    <load-on-startup>1</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>

 

 

登录页面代码

 

 

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="
http://struts.apache.org/tags-html" prefix="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 '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>
  <script type="text/javascript">
   function check(){
    var username=document.getElementById("userName").value;
    var password=document.getElementById("passWord").value;
    if(username.length<=0){
     alert("用户名不能为空");
     return false;
    }
    if(password.length<=0){
     alert("密码不能为空");
     return false;
    }
    if(username.valueOf()==" "){
     alert("用户名不能为空格")
     return false;
    }
    if(password.valueOf()==" "){
     alert("密码不能为空格")
     return false;
    }
    from1.submit();
   }
      function checks(){
    var username=document.getElementById("userName").value;
    var password=document.getElementById("passWord").value;
    if(username.length<=0){
     alert("用户名不能为空");
     return false;
    }
    if(password.length<=0){
     alert("密码不能为空");
     return false;
    }
    if(username.valueOf()==" "){
     alert("用户名不能为空格")
     return false;
    }
    if(password.valueOf()==" "){
     alert("密码不能为空格")
     return false;
    }
    alert("验证通过")
    document.loginForm.submit();
   }
  </script>
  <body>
  <form name="from1" action="${pageContext.request.contextPath}/login.do?operate=login" method="post" >
    username:<input type="text" name="userName"/><br>
    password:<input type="text" name="passWord"><br>
    <input type="button" name="button" value="tijiao" onclick="check()">
    </form>
    <html:form  action="/login.do?operate=login" method="post">
     username:<html:text property="userName" ></html:text>
     password:<html:password property="passWord"></html:password>
    <input type="button" name="button" value="提交" onclick="checks()">
    </html:form>
     
  </body>
</html>

 

action  代码

 

 

 public ActionForward login(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  LoginForm userForm = (LoginForm) form;// TODO Auto-generated method stub
  request.setAttribute("login", userForm);
  System.out.println(userForm.getPassWord());
  HttpSession session=request.getSession();
  request.setAttribute("list", listadd());
  session.setAttribute("users", userForm);
  if(userForm.getUserName().equals("quwei")){
   return mapping.findForward("ok");
  }else{
   return mapping.findForward("cuowu");
  }
  
 }

 

struts-config.xml

 

  <action-mappings >
    <action
      attribute="loginForm"
      name="loginForm"
      parameter="operate"
      path="/login"
      scope="request"
      type="com.cardvalue.view.action.LoginAction">
      <set-property property="cancellable" value="true" />
      <forward name="cuowu" path="/cuowu.jsp" />
      <forward name="ok" path="/ok.jsp" />
    </action>