ExtJs读取服务器端数据 spring+springJDBC+struts2+extjs技术

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/21611561

目录


效果:




extjs代码:

Ext.onReady(function() {
//			var data = [{
//						name : '唐伯虎',
//						age : 25,
//						sex : '男'
//					}, {
//						name : '楚留香',
//						age : 24,
//						sex : '女'
//					}];
	
			var ds = new Ext.data.JsonStore({

						proxy : new Ext.data.HttpProxy({
									url : 'StudentAction_findlist'// 相对路径
								}),
						root : ' ',
						fields : [{
									name : 'id'
								}, {
									name : 'name'
								}, {
									name : 'sex'
								}
								, {
									name : 'age'
								}, {
									name : 'phone'
								}, {
									name : 'address'
								}
								, {
									name : 'birthday'
								}]

					});

			// ds.loadData(data);
			var grid = new Ext.grid.GridPanel({
						viewConfig : {
							forceFit : true
						},
						store :ds,
						columns : [{
									header : '编号',
									dataIndex : 'id'
								}, {
									header : '名字',
									dataIndex : 'name'
								}, {
									header : '性别',
									dataIndex : 'sex'
								}, {
									header : '出生日期',
								  renderer:Ext.util.Format.dateRenderer("20y年m月d日"),
									dataIndex : 'birthday'
								}
								, {
									header : '手机号',
									dataIndex : 'phone'
								}, {
									header : '地址',
									dataIndex : 'address'
								}, {
									header : '年龄',
									dataIndex : 'age'
								}]
					});
			
			
			
			

			
			
			var win = new Ext.Window({
						title : 'HttpProxy',
						width : 900,
						height : 300,
						layout : 'fit',
						items :grid
					});
			ds.load();
		
			win.show();
		});

Action:
package com.ext.action;






import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import net.sf.json.JSONArray;


import org.apache.struts2.ServletActionContext;
import org.springframework.jdbc.core.JdbcTemplate;


import com.ext.pojo.student;
import com.opensymphony.xwork2.ActionSupport;


public class StudentAction extends ActionSupport {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	private JdbcTemplate jdbcTemplate;
	
    public void findlist(){
    	ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
    	try {
			ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	try {
			System.out.println("************************************************="+jdbcTemplate.getDataSource().getConnection());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	String json="[{";
         List  t_list= jdbcTemplate.queryForList("select * from student ;");
         for(int i=0;i<t_list.size();i++){
        	 Map  userMap=(Map) t_list.get(i);
			// userMap.get("id");
			// userMap.get("name");
			// userMap.get("sex");
			// userMap.get("age");
			// userMap.get("address");
			// userMap.get("phone");
			// userMap.get("birthday");
     /*   	 if (t_list.size()) {
        	
			} else {


			}*/
        	 json=json+"id:"+"\""+userMap.get("id")+"\""+",name:"+"\""+userMap.get("name")+"\""+",sex:"+"\""+userMap.get("sex")+"\""+",age:"+"\""+ userMap.get("age")+"\""+",address:"+"\""+userMap.get("address")+"\""+",phone:"+"\""+userMap.get("phone")+"\""+",birthday:"+"\""+userMap.get("birthday")+"\""+"}]";	
 // System.out.println(userMap.get("id"));   System.out.println(userMap.get("name"));   System.out.println(userMap.get("age"));


        	 }
    System.out.println("***********************"+json);
     	try {
			ServletActionContext.getResponse().getWriter().write(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         
    }
	
}
spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:context="http://www.springframework.org/schema/context"  
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
            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  
                ">  
                 <!-- 配置数据源 -->  
    <bean    class="org.apache.commons.dbcp.BasicDataSource"   
        destroy-method="close"  
        name="MydataSource">  
        <property name="driverClassName">  
            <value>com.mysql.jdbc.Driver</value>  
        </property>  
        <property name="url">  
            <value>jdbc:mysql://localhost:3306/test</value>  
        </property>  
        <property name="username">  
            <value>root</value>  
        </property>  
        <property name="password">  
            <value>123</value>  
        </property>  
    </bean>  
    
     <!-- 在jdbcTemplate模板中注入数据源 -->      
    <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="MydataSource"></property>     
    </bean>   
    
       <bean name="StudentAction" class="com.ext.action.StudentAction">  
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>     
    </bean>   
     </beans>  

    
    
    struts.xml


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

<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="chapter1" extends="struts-default">
   <action name="StudentAction_*" class="StudentAction" method="{1}">
    <result name="success">/WEB-INF/JspPage/chapter1/HelloWorld.jsp</result>
   </action>
</package>
</struts> 

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>ExtJsApplication</display-name>

 
 
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>WEB-INF/Spring*.xml</param-value>
</context-param>
 <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>
 
</web-app>

   




    pojo:

package com.ext.pojo;


import java.sql.Timestamp;


public class student {
private int id   ;
private String name   ;
private int  sex;
private int  age ;
private String phone  ;
private Timestamp birthday;
private String  address ;


//选修的课程号
private int courseId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Timestamp getBirthday() {
return birthday;
}
public void setBirthday(Timestamp birthday) {
this.birthday = birthday;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}








}
    
    index.html:

<html>
<head>
<title> extjs</title>
<link rel="stylesheet" type="text/css" href="js/resources/css/ext-all.css"/>
<script type="text/javascript" src="js/ext-all.js"></script>
<script type="text/javascript" src="js/application.js"></script>
</head>
<body>


</body>
</html>


    
    
    
    
相关文章
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
9165 132
|
存储 机器学习/深度学习 人工智能
硅谷GPU单节点服务器:技术解析与应用全景
“硅谷GPU单节点服务器”代表了在单个物理机箱内集成强大计算能力,特别是GPU加速能力的高性能计算解决方案。它们并非指代某个特定品牌,而是一类为处理密集型工作负载而设计的服务器范式的统称。
|
SQL Java 数据库连接
Spring Data JPA 技术深度解析与应用指南
本文档全面介绍 Spring Data JPA 的核心概念、技术原理和实际应用。作为 Spring 生态系统中数据访问层的关键组件,Spring Data JPA 极大简化了 Java 持久层开发。本文将深入探讨其架构设计、核心接口、查询派生机制、事务管理以及与 Spring 框架的集成方式,并通过实际示例展示如何高效地使用这一技术。本文档约1500字,适合有一定 Spring 和 JPA 基础的开发者阅读。
1073 0
|
前端开发 Java API
利用 Spring WebFlux 技术打造高效非阻塞 API 的完整开发方案与实践技巧
本文介绍了如何使用Spring WebFlux构建高效、可扩展的非阻塞API,涵盖响应式编程核心概念、技术方案设计及具体实现示例,适用于高并发场景下的API开发。
876 0
|
网络协议 关系型数据库 应用服务中间件
如何迁移网站数据到新的服务器
迁移网站数据到新服务器是一个系统化的过程,需谨慎操作以避免数据丢失或服务中断。小编为您整理发布如何迁移网站数据到新的服务器,以下是详细步骤和注意事项。
|
NoSQL Java 数据库连接
《深入理解Spring》Spring Data——数据访问的统一抽象与极致简化
Spring Data通过Repository抽象和方法名派生查询,简化数据访问层开发,告别冗余CRUD代码。支持JPA、MongoDB、Redis等多种存储,统一编程模型,提升开发效率与架构灵活性,是Java开发者必备利器。(238字)
|
监控 安全 Java
Spring Cloud 微服务治理技术详解与实践指南
本文档全面介绍 Spring Cloud 微服务治理框架的核心组件、架构设计和实践应用。作为 Spring 生态系统中构建分布式系统的标准工具箱,Spring Cloud 提供了一套完整的微服务解决方案,涵盖服务发现、配置管理、负载均衡、熔断器等关键功能。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
768 1
|
监控 Kubernetes Cloud Native
Spring Batch 批处理框架技术详解与实践指南
本文档全面介绍 Spring Batch 批处理框架的核心架构、关键组件和实际应用场景。作为 Spring 生态系统中专门处理大规模数据批处理的框架,Spring Batch 为企业级批处理作业提供了可靠的解决方案。本文将深入探讨其作业流程、组件模型、错误处理机制、性能优化策略以及与现代云原生环境的集成方式,帮助开发者构建高效、稳定的批处理系统。
1067 1
|
Java 数据库连接 开发者
Spring Framework 核心技术详解
本文档旨在深入解析 Java Spring Framework 的核心技术原理与应用。与侧重于快速开发的 Spring Boot 不同,本文将聚焦于 Spring 框架本身的设计理念、核心容器、控制反转(IoC)、面向切面编程(AOP)、数据访问与事务管理等基础且强大的模块。通过理解这些核心概念,开发者能够更深刻地领悟 Spring 生态系统的设计哲学,并具备解决复杂企业级应用开发问题的能力。
744 4
|
Java 应用服务中间件 开发者
Spring Boot 技术详解与应用实践
本文档旨在全面介绍 Spring Boot 这一广泛应用于现代企业级应用开发的框架。内容将涵盖 Spring Boot 的核心概念、核心特性、项目自动生成与结构解析、基础功能实现(如 RESTful API、数据访问)、配置管理以及最终的构建与部署。通过本文档,读者将能够理解 Spring Boot 如何简化 Spring 应用的初始搭建和开发过程,并掌握其基本使用方法。
947 2

热门文章

最新文章