struts2 extjs와 결합하여 이루어진 로그인 실례

9632 단어 struts2
1. struts2를 먼저 구축하면 myeclipse를 통해 신속하게 구축할 수 있습니다.
2. extjs에 필요한 라이브러리 파일을 다시 가져옵니다.
3. 실체 클래스 쓰기 User
package com.ext.model;

public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}

4. LoginAction 쓰기
package com.ext.action;

import com.ext.model.User;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private boolean success;
private String message;
private User user;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
if(user.getUsername().equals("admin")&&user.getPassword().equals("admin")){
this.success= true;
//this.message=" :"+user.getUsername()+" :"+user.getPassword();
}else{
this.success=false;
this.message=" , ";
}
return SUCCESS;
}

public boolean isSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

}

5. struts.xml은 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="extjs" extends="json-default" namespace="/">
<action name="Login" class="com.ext.action.LoginAction">
<result type="json"></result>
</action>
</package>
</struts>

6. 로그인.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="ext3/resources/css/ext-all.css"/>
<script type="text/javascript" src="ext3/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext3/ext-all.js"></script>
<script type="text/javascript" src="ext3/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="login.js"></script>
</head>
<body>
</body>
</html>

7. 로그인.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:'user.username',
allowBlank:false,
blankText:" !"
},{
xtype:'textfield',
fieldLabel:" ",
allowBlank:false,
blankText:" !",
name:'user.password',
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:'Login.action',
method:'post',
success:function(form,action){
document.location="index.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();

});

8. 로그인에 성공한 index 페이지는 쓰지 않습니다.
extjs로 페이지 사이의 점프를 실현하는 것은 좀 번거로우니 빨간색 부분을 주의하십시오.

좋은 웹페이지 즐겨찾기