<%@page contentType="text/html" pageEncoding="UTF-8"%> <% request.setAttribute("base", request.getContextPath()); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录界面</title> <link rel="stylesheet" type="text/css" href="${base}/ext/resources/css/ext-all.css" /> <script type="text/javascript" src="${base}/ext/ext-base.js"></script> <script type="text/javascript" src="${base}/ext/ext-all.js"></script> <script type="text/javascript" src="${base}/ext/ext-lang-zh_CN.js"></script><!--中文映射--> <script type="text/javascript"> function Login(){ //登录面板对象 var loginPanel=new Ext.form.FormPanel({ id:"loginPanel",labelPad:0,labelWidth:45,frame:true, items:[ {xtype:"field",id:"userName",fieldLabel:"用户名"}, {xtype:"field",id:"password",fieldLabel:"密 码"} ] }); var loginWindow; if(!loginWindow){ loginWindow=new Ext.Window({ id:"loginWindow", title:"登录窗口", width:210, height:127, resizable:false,//是否允许重新设置大小 closable:false,//是否允许关闭 items: [ loginPanel ], buttons: [ {xtype:"button",text:"确定",handler:validatorData}, {xtype:"button",text:"取消",handler:function(){ loginWindow.hide();//隐藏窗体 }} ] }); } loginWindow.show(); document.body.onkeydown=function(){ loginWindow.show(); }; //连接数据库 function validatorData(){ var userName=Ext.getCmp("userName").getValue();//根据id获取对象的值 var password=Ext.getCmp("password").getValue(); if(Ext.util.Format.trim(userName)=="" || Ext.util.Format.trim(password)==""){ Ext.Msg.alert("警告","请正确输入数据,用户名和密码都不能为空!"); } Ext.Ajax.request({ url:"login.action",//请求地址 params:{ParamUserName:userName,ParamPassword:password},//发送的参数 success:function(response,option){ var obj=Ext.decode(response.responseText);//将返回json字符串信息转为对象 if(obj.success==true)//判断返回的对象的属性是否为true { Ext.Msg.alert("好消息","登录成功"); //清除输入框 Ext.getCmp("userName").setValue(""); Ext.getCmp("password").setValue(""); loginWindow.hide(); } else{ Ext.Msg.alert("坏消息","坏消息:你登陆失败了!"); } }, failure:function(){ Ext.Msg.alert("坏消息","坏消息:你登陆出现异常了!"); } }) } //显示登录窗口 var btnShow=new Ext.Button({ renderTo:"btn",text:"显示登录窗口",handler:function(){ loginWindow.show(); } }) } Ext.onReady(Login) ;//参数为定义的函数,onReady是程序的入口 </script> </head> <body> <div id="btn" style=" text-align: center;padding-top: 300px; vertical-align: middle;"> </div> </body> </html>
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.cn.controler; import com.opensymphony.xwork2.ActionSupport; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; /** * * @author Administrator */ public class LoginAction extends ActionSupport { @Override public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); String userName = request.getParameter("ParamUserName"); String password = request.getParameter("ParamPassword"); //判断不为空 if ((!("").equals(userName) || userName != "") && (!("").equals(password) || password != "")) { if ("abc".equals(userName) && "abc".equals(password)) { PrintWriter out = response.getWriter(); out.write("{success:true}"); } else { PrintWriter out = response.getWriter(); out.write("{success:false}"); } } else{ PrintWriter out = response.getWriter(); out.write("{success:false}"); } return null; } }
<?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.action.extension" value="do" />--><!--将后缀改为.do的方式请求--> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <!-- <include file="mailreader-default.xml"/> <include file="mailreader-support.xml"/>--> <package name="struts2" namespace="/" extends="struts-default"> <action name="hello" class="com.cn.controler.HelloWorldAction" method="execute"> <result name="success">/hello.jsp</result> </action> <action name="login" class="com.cn.controler.LoginAction"></action> </package> </struts>