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>


    
    
    
    
相关文章
|
1月前
|
存储 搜索推荐 Java
|
2月前
|
存储 安全 PHP
通过eXtplorer+cpolar,搭建个人云存储并实现访问内网服务器数据
通过eXtplorer+cpolar,搭建个人云存储并实现访问内网服务器数据
36 0
|
4天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
19 3
|
4天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
20 3
|
19天前
|
弹性计算 网络安全 虚拟化
ECS数据问题之升级配置预防数据丢失如何解决
ECS(Elastic Compute Service,弹性计算服务)是云计算服务提供商提供的一种基础云服务,允许用户在云端获取和配置虚拟服务器。以下是ECS服务使用中的一些常见问题及其解答的合集:
|
22天前
|
安全 数据处理 C#
C# Post数据或文件到指定的服务器进行接收
C# Post数据或文件到指定的服务器进行接收
|
27天前
|
Java 数据库连接 数据库
Spring Boot整合MyBatis Plus集成多数据源轻松实现数据读写分离
Spring Boot整合MyBatis Plus集成多数据源轻松实现数据读写分离
26 2
|
1月前
|
消息中间件 关系型数据库 MySQL
Flink CDC产品常见问题之把flink cdc同步的数据写入到目标服务器失败如何解决
Flink CDC(Change Data Capture)是一个基于Apache Flink的实时数据变更捕获库,用于实现数据库的实时同步和变更流的处理;在本汇总中,我们组织了关于Flink CDC产品在实践中用户经常提出的问题及其解答,目的是辅助用户更好地理解和应用这一技术,优化实时数据处理流程。
|
1月前
|
Java 大数据 数据库
【Spring底层原理高级进阶】Spring Batch清洗和转换数据,一键处理繁杂数据!Spring Batch是如何实现IO流优化的?本文详解!
【Spring底层原理高级进阶】Spring Batch清洗和转换数据,一键处理繁杂数据!Spring Batch是如何实现IO流优化的?本文详解!
|
1月前
|
Cloud Native Java Docker
【Spring云原生】Spring官宣,干掉原生JVM,推出 Spring Native!整体提升性能!Native镜像技术在Spring中的应用
【Spring云原生】Spring官宣,干掉原生JVM,推出 Spring Native!整体提升性能!Native镜像技术在Spring中的应用