struts2结合extjs制作用户登陆功能

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyProject</display-name>

  <servlet>
    <description></description>
    <display-name>extLogin</display-name> <!-- extJS对应的servlet服务 -->
    <servlet-name>extLogin</servlet-name>
    <servlet-class>com.hik.user.Login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>extLogin</servlet-name>
    <url-pattern>/extLogin</url-pattern>
  </servlet-mapping>
  
 <!-- Struts2 filter -->
	<filter>
		<filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
            <init-param>
            <param-name>struts.action.extension</param-name>
            <param-value>action,php</param-value>  <!-- 设置拦截对象类型 -->
           </init-param>
	</filter>
	
<!--     <filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping> -->
	
     <filter-mapping> 
      <filter-name>struts2</filter-name> 
      <url-pattern>*.action</url-pattern>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <filter-mapping> 
      <filter-name>struts2</filter-name> 
      <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping> 
      <filter-name>struts2</filter-name> 
      <url-pattern>*.php</url-pattern>
    </filter-mapping>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
</web-app>

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Hello World!</title>
<link rel="stylesheet"  type="text/css" href="resources/js/ext3.2.1/resources/css/ext-all.css">
<!-- <link rel="stylesheet"  type="text/css" href="resources/css/xtheme-galdaka/css/xtheme-galdaka.css"> -->
<script type="text/javascript" src="resources/js/ext3.2.1/ext-base.js"></script>
<script type="text/javascript" src="resources/js/ext3.2.1/ext-all-debug.js"></script>
<script type="text/javascript" src="resources/js/ext3.2.1/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="resources/js/form2.js"></script>
</head>
<script type="text/javascript"> 
</script> 
</head> 
<body> 
<div id="button"></div> 
</body> 
</html> 

form2.js

Ext.onReady(function(){
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget="side";

var form1=new Ext.FormPanel({
    labelWidth:40,
    baseCls:'x-plain',
    defaults:{width:180},
    items:[{
        xtype:'textfield',
        fieldLabel:"用户名",
        id:"txtName",
        name:'username',
        allowBlank:false,
        blankText:"用户名不能为空!"
        },{
        xtype:'textfield',
        fieldLabel:"密码",
        allowBlank:false,
        blankText:"密码不能为空!",
        name:'pwd',
        inputType:'password'
        }],
    buttons:[{
        text:"提交",
        type:'submit',
        handler:function(){
            if(form1.getForm().isValid()) {
            Ext.MessageBox.show({
                title:'请等待',
                msg:'正在加载',
                progressText:'',
                width:300,
                progress:true,
                closable:'false',
                animEl:'loding'
            });    
            var f = function(v){
                return function(){
                    var i=v/11;
                    Ext.MessageBox.updateProgress(i,'');
                }
            }
            for(var i=1;i<33;i++){
                setTimeout(f(i),i*1500);
            }
            //提交到服务器操作
            form1.form.doAction('submit',{
            url:'UserLogin!login.action',
            method:'post',
            success:function(form,action){
                document.location="success.jsp";
                Ext.Msg.alert("登录成功!",action.result.message);
            },
            failure:function(form,action){
                Ext.Msg.alert("登录失败!",action.result.message);
            }
            });
        }
        }},
            {
        text:"重置",
        handler:function() {
        form1.getForm().reset();
}
}]
});

    var window = new Ext.Window({
        title :"登录窗口",
        layout:'fit',
        width:290,
        height:250,
        plain:true,
        bodyStyle:'padding:10px',
        maximizable:false,
        closeActon:'close',
        closable:false,
        collapsible:true,
        buttonAlign:'center',
        items:form1
    });
    window.show();

});


struts.xml

<?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>
	<!-- 动态地调用方法 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>  
    <package name="default" extends="json-default">
                <action name="UserLogin" class="com.hik.user.UserLoginAction">
        	 <result type="json"></result>
        </action>
    </package>

</struts>

UserLoginAction.java

package com.hik.user;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opensymphony.xwork2.ActionSupport;

public class UserLoginAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String username;
	private String pwd;
	private boolean success;
	private String message; 

	/**
	 * @return the pwd
	 */
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	
	
	/**
	 * @return the success
	 */
	public boolean isSuccess() {
		return success;
	}

	/**
	 * @param success the success to set
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}
	
	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String login() {
        
		System.out.println(username);
		System.out.println(pwd);
		 // TODO Auto-generated method stub  

	    if(username.equals("admin")&&pwd.equals("12345")){  
            this.success= true;  
            //this.message="你的账号是:"+user.getUsername()+"密码是:"+user.getPassword();  
        }else{  
            this.success=false;  
            this.message="对不起,未授权的用户不能登录改系统";  
        }  
        return SUCCESS;  

	}
}



 


 

 

 


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值