spring mvc实例带源码 json linechart
Spring 框架提供了构建 Web 应用程序 的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且包含多种视图 技术,例如 JavaServer Pages(JSP)技术、Velocity、Tiles、iText 和 POI。Spring MVC 框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。Spring MVC 分离了控制器、模型对象 、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
?
1.文档说明:
我的Csdn http://blog.csdn.net/zl563143188
spring3.1 http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/
struts2 http://struts.apache.org/2.x/docs/home.html
hibernate http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/
这是我的QQ空间,欢迎参观 http://user.qzone.qq.com/563143188
程序源码下载地址 10MB:http://115.com/file/angsm20g# (最新程序请下载附件 )
apache-tomcat -6.0.33请下载附件
程序lib下载地址 100MB:http://115.com/file/dp7u48os#lib.zip
数据库下载地址 (可以将数据转为sql,或者mysql,默认access)http://115.com/file/c29t2j70?
由于涉及技术比较多,一般很难运行成功,需要技术指导联系 QQ 563143188,电话 13823045912 张林
Struts2 、 Spring 、 Hibernate 三者整合的过程示例
SSH简单整合( Struts2 .2.3.1+ Spring3 .1.0+ Hibernate4)
Struts2 + Spring + Hibernate 搭建全解!
Hibernate4 Struts2 Spring3 整合
?
功能介绍:
1.支持 Hibernate连接Access Hibernate连接sqlsever Hibernate连接mysql
2.支持 Spring连接access数据库 spring连接mysql spring连接sqlsever数据库
3.支持 Spring动态数据源加载 Spring连接ibaits Spring JDBC连接
4.支持 Spring的事务管理 Spring AOP实现 Spring的权限管理
5.支持 Spring MVC及Spring国际化标签 Spring文件上传下载
6.支持 Spring 数据源加载属性文件
7.支持 struts2+hibernate4+spring3 及struts2+itbaits+spring设计结构
8.支持 ext+json+treepanel实现动态树
9.支持 jasperreport+ireport+excel打印
10.支持 freemarker 生成文件
11.支持 sitemesh修饰网站
12. 支持 oscahe缓存 hibernate+ehcache缓存
13. 支持 struts2国际化
14 支持 velocity模板设计
15.支持 ext+dwr+json处理数据
16.支持 jquery图表制作
17.支持 log4j日志处理
18.支持 hibernate与ibaits同时访问数据库
19.支持 java读dll,ocx控制
20.支持 SSH、SSI框架同时使用
21.支持 Spring实时调度任务
22.支持随意更改BaseDaoImpl<T extends BaseModel,PK extends BasePK> extends BaseHibernate4SpringDaoImpl<T,PK>
?
?
实现spring MVC的配置:
1.首先配置 web.xml ,在web.xml里面加入如下代码
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 为了修正struts2中上下文的错误 -->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
2.在webroot/web-inf/目录下面新建spring-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<!--/view/senlo/print/ 是指mvc返回时对应jsp的目录,返回的jsp放在其它目录返回无效-->
<bean
p:prefix="/view/senlo/print/" p:suffix=".jsp" />
<bean id="multipartResolver"
p:defaultEncoding="utf-8" />
</beans>
3.需要在applicationContext.xml中加入
<bean
/>
4.具体的控制层实现调用 ,实际上也是一个普通的类。
package com.senlo.analyze.main.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/extLine.do") //注明该类在应用上调用时为 exLine.do
public class ExtLineController {
@RequestMapping(params = "method=getCdata") //该类文类的对外名称
public void getCdata() {
try {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
response.setContentType("text/html; charset=utf-8");
PrintWriter out;
out = response.getWriter();
ArrayList<String> list=new ArrayList<String>();
String [] lStrings=new String []{"{name:1,visits:245000,views:300000,views2:400000,views3:300000}"};
list.add(lStrings[0]);
list.add("{name:2,visits:345000,views:430000,views2:550000,views3:330000}");
list.add("{name:3,visits:445000,views:400000,views2:380000,views3:330000}");
list.add("{name:4,visits:445000,views:500000,views2:380000,views3:420000}");
list.add("{name:5,visits:345000,views:400000,views2:730000,views3:670000}");
list.add("{name:6,visits:445000,views:590000,views2:630000,views3:770000}");
list.add("{name:7,visits:645000,views:620000,views2:580000,views3:490000}");
list.add("{name:8,visits:745000,views:600000,views2:50000,views3:400000}");
JSONArray JsonArray = JSONArray.fromObject(list); // 得到JSON数组
System.out.println(JsonArray.toString());
out.println(JsonArray.toString());
out = response.getWriter();
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(params = "method=getFiles")
public void getFiles() {
try {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
response.setContentType("text/html; charset=utf-8");
PrintWriter out;
System.out.println(request.getParameter("xy")+"++"+request.getAttribute("xy"));
out = response.getWriter();
ArrayList<String> listType=new ArrayList<String>();
ArrayList<String> listAttr=new ArrayList<String>();
listType.add("{name: 'name',type:'int'}");
listType.add("{name: 'visits',type:'int'}");
listType.add("{name: 'views',type:'int'}");
listType.add("{name: 'views2',type:'int'}");
listType.add("{name: 'views3',type:'int'}");
listAttr.add("{type: 'line', displayName: 'Good',yField: 'visits', style: {color:0x01BB00 } }");
listAttr.add("{type: 'line', displayName: 'Good',yField: 'views', style: {color:0x02BB00 } }");
listAttr.add("{type: 'line', displayName: 'Good',yField: 'views2', style: {color:0x03BB00 } }");
listAttr.add("{type: 'line', displayName: 'Good',yField: 'views3', style: {color:0x04BB00 } }");
JSONArray JsonArray1 = JSONArray.fromObject(listType); // 得到JSON数组
JSONArray JsonArray2 = JSONArray.fromObject(listAttr); // 得到JSON数组
System.out.println(JsonArray1.toString()+"==="+JsonArray2.toString());
out.println(JsonArray1.toString()+"==="+JsonArray2.toString());
out = response.getWriter();
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.具体的jsp调用spring mvc 路径 webroot/view/senlo/print/extline.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<link rel="stylesheet"
href="<%=basePath%>js/ext/resources/css/ext-all.css" type="text/css"></link>
<!-- link rel="stylesheet" href="<%=basePath%>js/ext/resources/css/xtheme-gray.css" type="text/css"></link-->
<script type="text/javascript"
src="<%=basePath%>js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<%=basePath%>js/ext/ext-all.js"></script>
<link href="<%=basePath%>/css/setperson.css" rel="stylesheet"
type="text/css" />
<link href="<%=basePath%>/css/table.css" rel="stylesheet"
type="text/css" />
<script type="text/javascript">
Ext.chart.Chart.CHART_URL = '<%=basePath%>js/ext/resources/charts.swf';
Ext.Ajax.request({
url:'<%=basePath%>extLine.do?method=getFiles ', //exLine.do spring MVC 就是类 getFilesspring MVC 就是方法
success : function(response) {
var json=response.responseText.split("===");
showGraph(json[0],json[1]);
},
failure : function(response) {
Ext.Msg.alert("读取数据出错","非常不好意思" );
}
});
function showGraph(json1,json2){
Ext.onReady(function(){
var dataType= Ext.data.Record.create(eval(json1));
var store = new Ext.data.Store({
proxy:new Ext.data.HttpProxy({url:'<%=basePath%>extLine.do?method=getCdata'
}),
reader : new Ext.data.JsonReader({}, dataType)
});
store.load();
// extra extra simple
new Ext.Panel({
title : "质控数据时时分析图",
applyTo : 'container',
width : 800,
height : 500,
layout : 'fit',
items : {
xtype : 'linechart',
store : store,
xField : 'name',
listeners : {
itemclick : function(o) {
var rec = store.getAt(o.index);
Ext.example.msg('Item Selected', 'You chose {0}.',
rec.get('name'));
}
},
series : eval(json2)
}
});
});
}
</script>
</head>
<body>
<div id='container'></div>
</body>
</html>
下面为利用spring mvc打印效果


?
jsp代码http://127.0.0.1:8088/myprj/view/xbs/crm/MyJsp.jsp
<%@ page language="java" contentType="text/html;charset=utf-8"%>
<%@taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<html>
?<head>
??<base >
<script type="text/javascript" src="http://127.0.0.1:8088/myprj/js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="http://127.0.0.1:8088/myprj/js/ext/ext-all.js"></script>
<script type="text/javascript" src="http://127.0.0.1:8088/myprj/js/public.js"></script>
<script type="text/javascript" src='http://127.0.0.1:8088/myprj/dwr/engine.js'></script>
<script type="text/javascript" src='http://127.0.0.1:8088/myprj/dwr/util.js'></script>
<script type="text/javascript" src="http://127.0.0.1:8088/myprj/js/render.js"></script>
<link?rel="stylesheet" type="text/css" href="http://127.0.0.1:8088/myprj/css/body.css">
<link ?rel="stylesheet" type="text/css" href="http://127.0.0.1:8088/myprj/css/table.css">
<link?rel="stylesheet" type="text/css" href="http://127.0.0.1:8088/myprj/css/button.css">
<link?rel="stylesheet" type="text/css" href="http://127.0.0.1:8088/myprj/js/ext/resources/css/ext-all.css" />
??<script type="text/javascript" src="http://127.0.0.1:8088/myprj/js/dwr/crm/chartLine.js"></script>
<script type="text/javascript">?
?
Ext.chart.Chart.CHART_URL = 'http://127.0.0.1:8088/myprj/js/ext/resources/charts.swf';
?
function initChart(selId){
? var andstr="";
? if(selId!=undefined)
? {
??? andstr=eval(selId);
? }
?// andstr="";
? var temp=chartLine_getLineData(andstr);
? var obj=document.getElementById("testId");
? obj.length=0;
?
?for(var i=0;i<temp.length;i++)
?{
?? var opt=document.createElement("OPTION");
?? opt.value=(i+1)+"";
?? opt.text=temp[i];
?? obj.add(opt);
?}
?
Ext.onReady(function(){?
?????? ?var cfiles="";?
???? ?cfiles=chartLine_getFiles(andstr);
???? ?var json= cfiles.split("===");???????????
??? ?var store = new Ext.data.Store({
??? ?proxy:new Ext.data.MemoryProxy(eval(chartLine_getCdata(andstr))),
????reader : new Ext.data.JsonReader({}, eval(json[0]))
???});
???store.load();
???// extra extra simple
???new Ext.Panel({
????title : "质控数据时时分析图",
????applyTo : 'container',
????width : 800,
????height : 500,
????layout : 'fit',
????items : {
?????xtype : 'linechart',
?????store : store,
?????xField : 'S0',
?????yField:'S8',
?????listeners : {
??????itemclick : function(o) {
???????var rec = store.getAt(o.index);
???????Ext.example.msg('Item Selected', 'You chose {0}.',
?????????rec.get('S0'));
??????}
?????},
????series :?? eval(json[1])
????}
???});
?
??});
?}?
</script>
</head>
<body? onload="initChart()">
<div id="mydiv"? >
??? <table class=maintable width="90%" align="center">???????????????
?????? <tr>
??????<td class=tabDetailViewDLLeft width="11%">
???????<font color="#FF0000">*</font>查询数据1
??????</td>
??????<td class=tabDetailViewDLRight width="30%"?? align="left">
??????? ?<input id=userId name="userId"?
????????style="color: black; width: 200px; height: 22px; PADDING-TOP: 2px">
??????</td>
??????<td class=tabDetailViewDLLeft width="13%">
??????? 查询数据2
??????</td>
??????<td class=tabDetailViewDLRight width="52%" align="left">
???????<input id=loginTime name="loginTime"?
????????style="color: black; width: 200px; height: 22px; PADDING-TOP: 2px">
??????</td>
?????</tr>
?????
?????<tr>
??????<td class=tabDetailViewDLLeft width="11%">
???????<font color="#FF0000">*</font> 查询数据3
??????</td>
??????<td class=tabDetailViewDLRight width="30%"?? align="left">
??????? ?<input id=userId name="userId"?
????????style="color: black; width: 200px; height: 22px; PADDING-TOP: 2px">
??????</td>
??????<td class=tabDetailViewDLLeft width="13%">
??????? 查询数据4
??????</td>
??????<td class=tabDetailViewDLRight width="52%" align="left">
???????<input id=loginTime name="loginTime"
????????style="color: black; width: 200px; height: 22px; PADDING-TOP: 2px">
??????</td>
?????</tr>
?????<tr>
??????<td class=tabDetailViewDLLeft width="11%">
???????<input type="button" value="查询数据" onclick="initChart()">
??????</td>
??????<td class=tabDetailViewDLRight width="30%"?? align="left">
??????? ?<input id=userId name="userId"
????????style="color: black; width: 200px; height: 22px; PADDING-TOP: 2px">
??????</td>
??????<td class=tabDetailViewDLLeft width="13%">
??????? <input type="button" value="取消查询" onclick="initChart()">
??????</td>
??????<td class=tabDetailViewDLRight width="52%" align="left">
???????<input id=loginTime name="loginTime"?
????????style="color: black; width: 200px; height: 22px; PADDING-TOP: 2px">
??????</td>
?????</tr>
?????
?????
??? </table>
??? </div>
? <table class=maintable width="80%" align="center">?
? <tr>
?????<td width="8%">
????? <select id="testId" size=35 multiple? onchange="initChart(this.value)"?? style="width:100" name="textid"><option value=-1>请选择</option>
?????
????? </select>??? <td>
?????<td colspan=3> <div id='container'></div></td>
?????</tr>
?</table>????
</body>
</html>
后台代码
?
package com.xbs.crm.crm.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.junit.Test;
public class ChartLine {
?String[] names = new String[] { "S0", "s1", "s2", "s3", "s4", "s5", "s6",
???"s7", "s8" }; // 控制的张数
?int[] data = new int[] { 34, 88, 22, 77, 33, 55, 64, 12, 32 };
?static String[][] lineData = new String[9][9];
?String[] colors = new String[] { "0xfaebd7", "0x00ffff", "0x0000ff",
???"0x8a2be2", "0xa52a2a", "0x7fff00", "0xff7f50", "0x6495ed",
???"0x008b8b", "0Xb8860b", "0X2f4f4f", "0Xffd700", "0Xadff2f",
???"0Xcd5c5c", "0Xff4500", "0XE75858", "0XE79F58", "0XE7E758",
???"0X9FE758", "0X58E7E7", "0X589FE7", "0X9F58E7", "0XDF20DF",
???"0X808080" };
?static {
??for (int i = 0; i < 9; i++) {
???for (int j = 0; j < 9; j++) {
????lineData[i][j] = (i + 1) * (j + 1) + "";
???}
??}
?}
?@Test
?public void test() {
??getCdata("");
??// getFiles("");
?}
?public String getCdata(String and) {
??ArrayList<String> list = new ArrayList<String>();
??StringBuffer temp;
??for (int i = 0; i < data.length; i++) { //这里是确定的点数
???if (and.equals("") || (i == 0 || i == data.length - 1)) {
????temp = new StringBuffer();
????temp.append("{");
????for (int j = 0; j < names.length; j++) { //这里需要取了出实际有多少点的数据,也就是X轴的数据
?????if (and.equals("") || (j == 0 || j == data.length - 1)
???????|| and.equals("" + (j + 1))) {
??????temp.append(names[j] + ":" + lineData[i][j] + ",");
?????}
????}
????list.add(temp.toString().substring(0,
??????temp.toString().length() - 1)
??????+ "}");
???}else if (and.equals("" + (i + 1)))? {
????for (int k = 1; k < data.length-1; k++) {????
????temp = new StringBuffer();
????temp.append("{");
????for (int j = 0; j < names.length; j++) { //这里需要取了出实际有多少点的数据,也就是X轴的数据
?????if (and.equals("") || (j == 0 || j == data.length - 1)
???????|| and.equals("" + (j + 1))) {
??????temp.append(names[j] + ":" + lineData[k][j] + ",");
?????}
????}
????list.add(temp.toString().substring(0,
??????temp.toString().length() - 1)
??????+ "}");
????}
???}
??}
??JSONArray JsonArray = JSONArray.fromObject(list); // 得到JSON数组
??System.out.println(JsonArray.toString());
??return JsonArray.toString();
?}
? /*
?? * [{"S0":1,"s5":6,"s8":9},{"S0":2,"s5":12,"s8":18},{"S0":3,"s5":18,"s8":27},{"S0":4,"s5":24,"s8":36},{"S0":5,"s5":30,"s8":45},{"S0":6,"s5":36,"s8":54},{"S0":7,"s5":42,"s8":63},{"S0":8,"s5":48,"s8":72},{"S0":9,"s5":54,"s8":81}]
?? *
?? * [{"name":"S0","type":"int"},{"name":"s5","type":"int"},{"name":"s8","type":"int"}]===[{"type":"line","displayName":"s5","yField":"s5","style":{"color":8388352}}]
?? * */
?public String getFiles(String and) {
??ArrayList<String> listType = new ArrayList<String>();
??ArrayList<String> listAttr = new ArrayList<String>();
??for (int i = 0; i < names.length; i++) {
???if (and.equals("") || (i == 0 || i == data.length - 1)
?????|| and.equals("" + (i + 1))) {
????listType.add("{name: '" + names[i] + "',type:'int'}");
????if (i > 0 && i < names.length - 1) {
?????listAttr.add("{type: 'line',? displayName: '" + names[i]
???????+ "',yField: '" + names[i] + "', style: {color:"
???????+ colors[i] + " } }");
????}
???}
??}
??JSONArray JsonArray1 = JSONArray.fromObject(listType); // 得到JSON数组
??JSONArray JsonArray2 = JSONArray.fromObject(listAttr); // 得到JSON数组
??System.out.println(JsonArray1.toString() + "==="
????+ JsonArray2.toString());
??return JsonArray1.toString() + "===" + JsonArray2.toString();
?}
?public List<String> getLineData(String and) {
??List<String> list = new ArrayList<String>();
??/*list.add("S1");
??list.add("S2");
??list.add("S3");
??list.add("S4");
??list.add("S5");
??list.add("S6");
??list.add("S7");
??list.add("S8");*/
??list.add("ALT");
??list.add("AST");
??list.add("ALB");
??list.add("TP");
??list.add("DB");
??list.add("BUN");
??list.add("CHO");
??list.add("IB");
??return list;
?}
}
dwr代码
document.write("<script src='http://127.0.0.1:8088/myprj/dwr/interface/chartLine.js'></script>");
function chartLine_getCdata(and){
??? var temp;DWREngine.setAsync(false);
??? chartLine.getCdata(and,function(data){temp=data});
??? DWREngine.setAsync(true);return temp;
}
function chartLine_getFiles(and){
??? var temp;DWREngine.setAsync(false);
??? chartLine.getFiles(and,function(data){temp=data});
??? DWREngine.setAsync(true);return temp;
}
function chartLine_getLineData(and){
??? var temp;DWREngine.setAsync(false);
??? chartLine.getLineData(and,function(data){temp=data});
??? DWREngine.setAsync(true);return temp;
}
?
?
?
6.好了springmvc 就这么简单的实现,大家研究一下我的代码就明白了
?
由于配置文件太多 详细说明 :http://zz563143188.iteye.com/admin/blogs/1462413
?