一.前端 :登陆页面

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-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%>">
   
   
  </head>
  <body>
  <html:errors/>

 <form action="loginAction.do" method="post"> 
    请您登陆,输入用户名和密码 <br>
    用户名:<input type="text" name="username"><html:errors property="user.username.null"/><br/>
    密码 :<input type="text" name="password"><html:errors property="user.password.null"/><br/>
    <input type="submit" value="提交">&nbsp;<input type="reset" value="重置"><br>
    </form>
  </body>
</html>

后台步骤:

一。Action类
1. extends DispatchAction;

2.重写ActionForward execute方法

 Users user=(Users)form;//强制类型转换
 return mapping.findForward("SUCCESS");

action类如下:

package com.action;

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;

import com.entity.Users;
public class LoginAction extends DispatchAction{
 public ActionForward execute(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,HttpServletResponse response)
 throws Exception{
  Users user=(Users)form;
  return mapping.findForward("SUCCESS");
 }
}


 
二。实体类extends ActionForm如下:

package com.entity;

import org.apache.struts.action.ActionForm;

public class Users 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;
 }
 
}


三。配置struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <form-beans>
  <form-bean name="users" type="com.entity.Users">
  </form-bean>
  </form-beans>
 
  <global-exceptions/>
  <global-forwards />


   <action-mappings>
   <action path="/loginAction" type="com.action.LoginAction" name="users">
   <forward name="SUCCESS" path="/success.jsp"></forward>
   </action>
   </action-mappings>


</struts-config>

---------------------------------------------------------------------------------------------------------------------------------------

验证的两种方式

方法一。实体类继承ActionForm并重写验证方法

1.实体Users类如下:

package com.entity;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class Users 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;
 }
 public ActionErrors validate(ActionMapping mapping,
            javax.servlet.http.HttpServletRequest request){
  ActionErrors error=new ActionErrors();//继承自ActionMessages类,信息集合
  if(this.getUsername()==null||this.getUsername().trim().equals("")){
   error.add("user.username.null", new ActionMessage("user.username.null"));
  }
  if(this.getPassword()==null||this.getPassword().trim().equals("")){
   error.add("user.password.null", new ActionMessage("user.password.null"));
  }
  
  return  error;
  
 }
 
}

2.LoginAction类

public class LoginAction extends DispatchAction{
 public ActionForward execute(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,HttpServletResponse response)
 throws Exception{
  
  return mapping.findForward("SUCCESS");
 }
}

方法二.Action类中写验证

LoginAction类代码如下:

public class LoginAction extends DispatchAction{
 public ActionForward execute(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,HttpServletResponse response)
 throws Exception{
  Users user=(Users)form;
  ActionMessages messages=new ActionMessages();//信息集合
  if(user.getUsername()==null||user.getUsername().trim().equals("")){
   messages.add("user.username.null", new ActionMessage("user.username.null"));//国际化
   //this.addErrors(request, messages);
   //return mapping.findForward("INPUT");
  }
  if(user.getPassword()==null||user.getPassword().trim().equals("")){
   messages.add("user.password.null", new ActionMessage("user.password.null"));
   //this.addErrors(request, messages);
   //return mapping.findForward("INPUT");
  }
  if(messages.size()>0){
   this.addErrors(request, messages);
   return mapping.getInputForward();
   //return mapping.findForward("INPUT");
   
  }
  return mapping.findForward("SUCCESS");
 }
}

3.struts-config.xml中<action>内容修改如下:

 <action input="/index.jsp" path="/loginAction" type="com.action.LoginAction" name="users">
   <forward name="SUCCESS" path="/success.jsp"></forward>
   </action>

 

posted on 2013-08-12 10:27  慧是永远  阅读(188)  评论(0编辑  收藏  举报