目录
效果:
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>