struts1-struts2的login实例比较(附源代码)

本文参考:

http://www.blogjava.net/nokiaguy/archive/2008/04/15/193229.html

 

先是web.xml

代码
<filter>
    
<filter-name>struts2</filter-name>
    
<filter-class>
        org.apache.struts2.dispatcher.FilterDispatcher            
    
</filter-class>
</filter>
<filter-mapping>
    
<filter-name>struts2</filter-name>
    
<url-pattern>/*</url-pattern>
</filter-mapping>

 

UserLoginAction.java  struts 1.x

 

代码
package com.myeclipseide.examples.struts.action;

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 com.myeclipseide.examples.struts.form.UserLoginForm;

public class UserLoginAction extends Action {
    @Override
    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserLoginForm userLoginForm 
= (UserLoginForm) form;

        
if (userLoginForm.getUserName().equalsIgnoreCase("myeclipse")
                
&& userLoginForm.getPassword().equalsIgnoreCase("myeclipse")) {
            request.setAttribute(
"userName", userLoginForm.getUserName());
            
return mapping.findForward("success");
        }

        
return mapping.findForward("failure");

    }
}

 

 

UserLoginAction.java   struts2.x

 

代码
/**
 * UserLoginAction.java 2.x
 * 
*/
package com.myeclipseide.examples.struts.action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings(
"serial")
public class UserLoginAction extends ActionSupport {
    
private String userName;
    
private String passwd;
    @Override
    
public String execute(){
        
if("xinyu126".equals(userName)){
            
if("xinyu126".equals(passwd)){
                
return "success";
            }
            
return "WrongName";
        }
        
return "WrongPass";
    }
    
//一下这些在struts 1.x 中是在另一个java文件中单独写出来的。
    public void setUserName(String s){
        userName
=s;
    }
    
public String getUserName(){
        
return userName;
    }
    
public void setPasswd(String s){
        passwd
=s;
    }
    
public String getPasswd(){
        
return passwd;
    }
}

 

 

UserLoginForm.java 1.x

 

代码

//这个java类只在1.x版本中有,在2.x中已经和在一起了。在上面的一个文件中可以看到

package com.myeclipseide.examples.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class UserLoginForm extends ActionForm {
    
private static final long serialVersionUID = 1L;

    
private String password;

    
private String userName;
/*    
 * 重写validate 和 reset 方法,
    @Override 
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

        if (getUserName() == null || getUserName().length() < 1) {
            errors.add("userName", new ActionMessage("error.name.required"));
        } else if (!getUserName().equalsIgnoreCase("myeclipse")) {
            errors.add("userName", new ActionMessage("error.name.myeclipse"));
        }
        if (getPassword() == null || getPassword().length() < 1) {
            errors
                    .add("password", new ActionMessage(
                            "error.password.required"));
        } else if (!getPassword().equalsIgnoreCase("myeclipse")) {
            errors.add("password",
                    new ActionMessage("error.password.myeclipse"));
        }

        return errors;

    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        password = null;
        userName = null;
    }
*/
    
public String getPassword() {
        
return password;
    }

    
public void setPassword(String password) {
        
this.password = password;
    }

    
public String getUserName() {
        
return userName;
    }

    
public void setUserName(String userName) {
        
this.userName = userName;
    }
}

 

userLogin.jsp 1.x

 

 

代码
<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
    
<html:base />

    
<title>JSP for UserLoginForm form</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>
    
<html:form action="/userLogin">
        
<table border="1" cellpadding="4">
            
<tbody>
                
<tr>
                    
<td align="right">
                        User Name:
                    
</td>
                    
<td>
                        
<!----------------------->
                        
<html:text property="userName" />
                    
</td>
                    
<td>
                        
<!----------------------->
                        
<html:errors property="userName" />
                    
</td>
                
</tr>
                
<tr>
                    
<td align="right">
                        Password:
                    
</td>
                    
<td>
                        
<!----------------------->
                        
<html:password property="password" />
                    
</td>
                    
<td>
                        
<html:errors property="password" />
                    
</td>
                
</tr>
                
<tr>
                    
<td>
                        
&nbsp;
                    
</td>
                    
<td>
                        
<!----------------------->
                        
<html:submit />
                        
<html:cancel />
                    
</td>
                
</tr>
            
</tbody>
        
</table>
    
</html:form>
</body>
</html:html>

 

 

LoginSuccess.jsp 1.x

 

代码
<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html> 
  
<head> 
    
<title>My Struts 'userLoginSuccess.jsp' ending page</title> 
  
</head> 
  
  
<body> 
    Hello 
<bean:write name="userName" scope="request" />, you successfully logged in! 
  
</body> 
</html:html> 

 

 

struts-config.xml 1.x

代码
<?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="userLoginForm"
            type
="com.myeclipseide.examples.struts.form.UserLoginForm" />
    
</form-beans>
    
<global-exceptions />
    
<global-forwards />
    
<action-mappings>
        
<action attribute="userLoginForm" input="/userLogin.jsp"
            name
="userLoginForm" path="/userLogin" scope="request"
            validate
="true"
            type
="com.myeclipseide.examples.struts.action.UserLoginAction">
            
<forward name="success" path="/userLoginSuccess.jsp" />
            
<forward name="failure" path="/userLogin.jsp" />
        
</action>
    
</action-mappings>
    
<message-resources
        
parameter="com.myeclipseide.examples.struts.ApplicationResources" />
</struts-config>

 

 

 

 userlogin.jsp 2.x

 

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="/struts-tags" prefix="s"%>
<!-- 这里把struts1.x中的多个tags集中在一起了。"/struts-tags "-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


    
<title>JSP for UserLoginForm form</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>
    
<s:form name="loginform" method="post" action="login" namespace="/login">
<!--action: struts.xml 中的 action name ,namespace: package的 namespace 属性名-->
        
<s:textfield name="userName" label="userName:" />
        
<s:password name="passwd" label="passwd:" />
        
<s:submit value="submit"/>
    
</s:form>
</body>
</html>

 

struts 2.x的配置文档。

注意:这个应该放在web-inf下的classes文件夹里面。文件名为struts.xml

classes目录在Eclipse下是看不到的。需要手动拷贝过去。

而1.x版本是直接在web-inf目录下。

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

<struts>
    
<include file="struts-default.xml"></include>
    
<package name="struts_login" namespace="/login" extends="struts-default">
        
<action name="login" class="com.myeclipseide.examples.struts.action.UserLoginAction" >
            
<result name="success">/userLoginSuccess.jsp</result>
            
<result name="WrongName">/userLogin.jsp</result>
<!--这个name属性就是前面的action(java)execute方法的返回值,通过不同的返回值调入不用的结果页面-->
            
<result name="WrongPass">/userLogin.jsp</result>
        
</action>
    
</package>
    
</struts>

 

注意:package.name是唯一的名字。

   package.namespace是和url属性有关的,这个需要和前面的<s:form>标签的namespace相同

   action的name要和<s:form>的action值相同。

 

最后将这个项目下的webroot文件夹都拷贝到tomcat的webroot下。启动tomcat

输入http://localhost:8080/【webroot】/userLogin.jsp

这里这个webroot就是你拷贝进去的文件夹名,可以修改成别的名。这个文件夹和tomcat的webroot不要弄混了

 

源代码下载

/Files/xinyu126/StrutsLoginExample.rar

 

 

 

 /Files/xinyu126/StrutsLoginExample.rar

posted @ 2009-12-06 15:22  vva  阅读(1994)  评论(0)    收藏  举报