[b]主要功能[/b]:使用ext form提交表单
由struts2 action处理逻辑,并返回json数据
ext接收返回的json数据给出提示
使用spring管理bean
[b]主要配置:[/b] 引入struts的json插件jar包,在struts的配置文件中配置为extends="json-default"。
action需返回如"{success:true,msg:'返回信息'}"的JSON字符
[b]1 表单所在jsp(user_regist.jsp)[/b]
[b]2 user_regist.js[/b]
[b]3 action类(com.test.Login)[/b]
[b]4 struts配置文件struts_user.xml[/b]
[b]其它配置
applicationContext.xml[/b]
[b]struts.xml[/b]
[b]web.xml[/b]
引入的jar包
见附件图片
ext版本 3.3
文件路径结构见附件图片
由struts2 action处理逻辑,并返回json数据
ext接收返回的json数据给出提示
使用spring管理bean
[b]主要配置:[/b] 引入struts的json插件jar包,在struts的配置文件中配置为extends="json-default"。
action需返回如"{success:true,msg:'返回信息'}"的JSON字符
[b]1 表单所在jsp(user_regist.jsp)[/b]
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>User Regist!</title>
<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css"/>
<!-- GC -->
<!-- LIBS -->
<script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script>
<!-- ENDLIBS -->
<script type="text/javascript" src="extjs/ext-all.js"></script>
<script type="text/javascript" src="user_regist.js"></script>
</head>
<body>
<div id="login"></div>
</body>
</html>
[b]2 user_regist.js[/b]
/*
* Ext JS Library 2.3.0
* Copyright(c) 2006-2009, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.onReady(function() {
var form = new Ext.form.FormPanel({
baseCls: 'x-plain',
layout:'absolute',
url:'login.action',//处理表单提交的action路径
defaultType: 'textfield',
items: [{
x: 0,
y: 5,
xtype:'label',
text: '用户名:'
},{
x: 60,
y: 0,
name: 'userName',
anchor:'100%' // anchor width by percentage
},{
x: 0,
y: 35,
xtype:'label',
text: '密码:'
},{
x: 60,
y: 30,
name: 'pwd',
inputType:'password',
anchor: '100%' // anchor width by percentage
}
]
});
var window = new Ext.Window({
title: 'Resize Me',
width: 500,
height:300,
minWidth: 300,
minHeight: 200,
layout: 'fit',
plain:true,
bodyStyle:'padding:5px;',
buttonAlign:'center',
items: form,
buttons: [{
text: 'log in',
formBind:true,
handler:function(){
form.getForm().submit(
{
method:"POST",
url:"login.action",
waitMsg:"等着吧……",
waitTitle:"doing",
failure:function(form,action){
Ext.MessageBox.alert("失败",action.result.msg);
form.reset();
},
success:function(form,action){
Ext.MessageBox.alert("login ok",
action.result.msg,
function(btn,text){
var redirect = "要跳转的页面地址";
window.location=redirect;
}
);
}
}
);
}
},{
text: 'Cancel'
}]
});
window.show();
});
[b]3 action类(com.test.Login)[/b]
package com.test;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
/*返回给ext的成功标志,不要设成String类型的了*/
private boolean success;
/*返回给ext的处理结果*/
private String msg;
/*以parameter形式从表单获得userName字段*/
private String userName;
/*以parameter形式从表单获得pwd字段*/
private String pwd;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String execute() throws Exception {
//System.out.println("-------用户名"+userName);
if("gaoshun".equals(userName)&&"123".equals(pwd)){
setSuccess(true);
setMsg("ok!!!!!!!!!!!!!!!!!!!!!!");
}else{
setSuccess(false);
setMsg("error!!!!!!!!!!!!!!!!!!!");
}
return SUCCESS;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
[b]4 struts配置文件struts_user.xml[/b]
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--注意这里的json-default是action能自动返回json字符的原因之一-->
<package name="login" extends="json-default" namespace="/">
<action name="login" class="login">
<result type="json"/>
</action>
</package>
</struts>
[b]其它配置
applicationContext.xml[/b]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="login" class="com.test.Login"></bean>
class="com.test.service.impl.UserDAOImpl"></bean>
<!-- 日志时间打印 -->
<aop:config>
<!-- Spring 2.0 可以用 AspectJ 的语法定义 Pointcut,这
里拦截 service 包中的所有方法,和本示例没关系 -->
<aop:pointcut id="interceptorPointCuts"
expression="execution(* *..service..*.reg*(..))" />
<aop:advisor advice-ref="methodTimeAdvice"
pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>
[b]struts.xml[/b]
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="objectFactory" value="spring"></constant>
<include file="struts_*.xml"/>
</struts>
[b]web.xml[/b]
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>openviewEnv</display-name>
<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>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<jsp-config>
<taglib>
<taglib-uri>/struts-tags</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
引入的jar包
见附件图片
ext版本 3.3
文件路径结构见附件图片