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天前
|
安全 Linux 文件存储
如何在本地服务器部署TeslaMate并远程查看特斯拉汽车数据无需公网ip
如何在本地服务器部署TeslaMate并远程查看特斯拉汽车数据无需公网ip
|
1天前
|
JSON Android开发 数据格式
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
19 2
|
1天前
|
监控 Java 测试技术
【技术面试】服务器常问面试集锦
【技术面试】服务器常问面试集锦
9 2
|
1天前
|
数据采集 中间件 Python
Scrapy爬虫:利用代理服务器爬取热门网站数据
Scrapy爬虫:利用代理服务器爬取热门网站数据
|
1天前
|
存储 算法 数据挖掘
服务器数据恢复—拯救raid5阵列数据大行动,raid5数据恢复案例分享
**Raid5数据恢复算法原理:** 分布式奇偶校验的独立磁盘结构(被称之为raid5)的数据恢复有一个“奇偶校验”的概念。可以简单的理解为二进制运算中的“异或运算”,通常使用的标识是xor。运算规则:若二者值相同则结果为0,若二者结果不同则结果为1。 例如0101 xor 0010根据上述运算规则来计算的话二者第一位都是0,两者相同,结果为0 ;第二、三、四位的数值不同则结果均为1,所以最终结果为0111。公式表示为:0101 xor 0010 = 0111,所以在 a xor b=c 中如果缺少其中之一,我们可以通过其他数据进行推算,这就是raid5数据恢复的基本原理。 了解了这个基本原理
|
1天前
|
存储 安全 数据安全/隐私保护
Web应用程序的会话管理是一种跟踪和识别特定用户与Web服务器之间交互的技术
【5月更文挑战第12天】Python Web开发中,会话管理用于跟踪用户与服务器交互,如Flask框架提供的内置功能。以下是一个简单示例:安装Flask后,设置应用密钥,通过session读写用户状态。例如,创建一个显示和设置用户名称的Web应用,用户提交的名字将保存在会话中。在生产环境中,应安全存储密钥,如使用环境变量。扩展会话管理可借助第三方库实现更多功能,但可能需更多配置。
93 2
|
1天前
LabVIEW使用VI服务器的调用节点将数据传递到另一个VI 使用调用节点(Invoke Node)与通过引用调用节点(Call by Reference)调用VI时有什么差别?
LabVIEW使用VI服务器的调用节点将数据传递到另一个VI 使用调用节点(Invoke Node)与通过引用调用节点(Call by Reference)调用VI时有什么差别?
|
1天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
24 0
|
1天前
|
SQL Java 数据库连接
Springboot框架整合Spring JDBC操作数据
JDBC是Java数据库连接API,用于执行SQL并访问多种关系数据库。它包括一系列Java类和接口,用于建立数据库连接、创建数据库操作对象、定义SQL语句、执行操作并处理结果集。直接使用JDBC涉及七个步骤,包括加载驱动、建立连接、创建对象、定义SQL、执行操作、处理结果和关闭资源。Spring Boot的`spring-boot-starter-jdbc`简化了这些步骤,提供了一个在Spring生态中更便捷使用JDBC的封装。集成Spring JDBC需要添加相关依赖,配置数据库连接信息,并通过JdbcTemplate进行数据库操作,如插入、更新、删除和查询。
|
1天前
|
SQL Java 数据库连接
Springboot框架整合Spring Data JPA操作数据
Spring Data JPA是Spring基于ORM和JPA规范封装的框架,简化了数据库操作,提供增删改查等接口,并可通过方法名自动生成查询。集成到Spring Boot需添加相关依赖并配置数据库连接和JPA设置。基础用法包括定义实体类和Repository接口,通过Repository接口可直接进行数据操作。此外,JPA支持关键字查询,如通过`findByAuthor`自动转换为SQL的`WHERE author=?`查询。

热门文章

最新文章