Java Web框架DIYWord格式文档下载.docx
- 文档编号:18662771
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:16
- 大小:19.70KB
Java Web框架DIYWord格式文档下载.docx
《Java Web框架DIYWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《Java Web框架DIYWord格式文档下载.docx(16页珍藏版)》请在冰豆网上搜索。
sqlitejdbc-v053.jar,
sqlite的jdbc驱动,因为只是做一个demo所以用了一个我知道的最简单的数据库。
json_param.js算是一个jquery插件。
把dom中的内容做成json;
包装了一下jQuery.ajax,使其调用更简单。
//Addadomparametertojson.
$(document).ready(function(){
jQuery.jsonParam={
jsonRetn:
newObject(),
registParaToJson:
function(dom){
if(dom.is("
table"
)){
vararryData=newArray();
$('
tr:
gt(0)'
dom).each(function(){
varcurRow=newObject();
td'
$(this)).each(function(index){
if($(this).children().is("
input[type=text]"
curRow[index]=$('
input[type=text]:
eq(0)'
this).val();
}
elseif($(this).children().is("
input[type=radio]"
input[type=radio]:
this).attr("
checked"
);
input[type=checkbox]"
input[type=checkbox]:
select"
select:
else{
curRow[index]="
"
;
});
arryData.push(curRow);
$.jsonParam.jsonRetn[dom.attr('
id'
)]=arryData;
elseif(dom.is("
)||dom.is("
)]=dom.val();
)]=dom.attr("
},
passEventToJson:
function(even){
$.jsonParam.jsonRetn.passEvent=even;
getParamFromJson:
function(){
returnJSON.stringify($.jsonParam.jsonRetn);
ajaxJson:
function(url,callback){
returnjQuery.ajax({
type:
"
post"
url:
url,
dataType:
json"
data:
{'
param'
:
$.jsonParam.getParamFromJson()},
success:
callback
});
first.jsp一个jsp,注意$.jsonParam.registParaToJson往后台传值的使用。
<
%@pagelanguage="
java"
contentType="
text/html;
charset=utf-8"
%>
!
DOCTYPEhtmlPUBLIC"
-//W3C//DTDXHTML1.0Transitional//EN"
>
html>
head>
<
title>
/title>
scriptsrc="
script/jquery.js"
type="
text/javascript"
>
/script>
script/json2.js"
script/json_param.js"
scripttype="
$('
#contentdiv'
).html('
&
nbsp;
'
$("
#idm"
).change(function(){
$.jsonParam.passEventToJson('
idm'
$.jsonParam.registParaToJson($('
#idm'
));
$.jsonParam.ajaxJson("
SecondServlet"
function(response){
).empty();
$.each(response,function(entryIdex,entry){
).append(entry['
MAIN_DISH'
]);
);
$("
#b_pass"
).click(function(){
b_pass'
#t_demo'
//~$.jsonParam.ajaxJson("
//~function(response){
//~$('
#ret_table'
//~varhtm='
tableborder="
1"
//~$.each(response,function(entryIdex,entry){
//~htm+='
tr>
td>
+entry['
0'
]+'
/td>
1'
2'
/tr>
//~});
/table>
).append(htm);
//~}
//~);
#param"
).attr("
value"
$.jsonParam.getParamFromJson());
#form1"
action"
"
).submit();
returntrue;
/head>
body>
DIVid=main>
formid="
form1"
method="
action="
divid="
databox"
selectid="
idm"
optionvalue="
0"
selected="
selected"
What'
sforDinner?
...<
/option>
Monday<
2"
Tuesday<
3"
Wednesday<
4"
Thursday<
5"
Friday<
6"
Saturday<
/select>
pid="
contentdiv"
/p>
/div>
div>
inputtype="
button"
value="
pass"
id="
b_pass"
class="
test_button"
/>
br/>
tableid="
t_demo"
border="
th>
No.<
/th>
content<
select<
chk<
radio"
name="
checked="
text"
size="
12"
selectname="
Cats"
----<
Calico<
Tortie<
Siamese<
checkbox"
hidden"
param"
/>
ret_table"
/form>
/DIV>
/body>
/html>
SecondServlet.java,后台Servlet,注意jreq.GetParameterFromJson中的参数,他的命名就是jsp中dom的id名,约定大于配置。
packageorg.demo;
importjava.io.*;
importjava.util.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
importmon.*;
/**
*Thesimplestpossibleservlet.
*
*@authorchengj
*/
publicclassSecondServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsIOException,ServletException{
doPost(request,response);
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,ServletException{
Stringparam=request.getParameter("
JsonRequestjreq=newJsonRequest(param);
StringresWrite="
SecondEntyenty=newSecondEnty();
if("
.equals((String)jreq.GetParameterFromJson("
passEvent"
)))
{
Stringkey=(String)jreq.GetParameterFromJson("
resWrite=enty.getDish(key);
response.setContentType("
application/json;
charset=utf-8"
PrintWriterout=response.getWriter();
out.println(resWrite);
elseif("
))){
resWrite=enty.getDetail((ArrayList)jreq.GetParameterFromJson("
request.setAttribute("
resWrite);
RequestDispatcherrd=request.getRequestDispatcher("
/jsp/second.jsp"
rd.forward(request,response);
}
SecondEnty.java,SecondServlet的业务相关类。
importjava.sql.*;
publicclassSecondEntyextendsDBAccess{
privateConnectionconn=null;
publicStringgetDish(Stringkey){
StringretJson=null;
try{
PreparedStatementprep=super.conn.prepareStatement("
SELECTday,main_dishFROMmenuwhereqsid=?
prep.setString(1,key);
retJson=JsonResponse.GetQuery(prep.executeQuery());
returnretJson;
}catch(Exceptione){
System.out.println(e.toString());
returnnull;
publicStringgetDetail(ArrayListtable){
ArrayListretTable=newArrayList();
for(inti=0;
i<
table.size();
i++){
ArrayListarrRow=(ArrayList)table.get(i);
if(arrRow.get(3).equals("
true"
retTable.add(table.get(i));
returnJsonResponse.GetTable(retTable);
下面是两个公共类,负责request到json,以及json到response的转换任务
packagemon;
importorg.json.*;
publicclassJsonRequest{
privateJSONObjectdata;
publicJsonRequest(Stringparam){
this.data=newJSONObject(param);
}catch(JSONExceptione){
System.out.println(e.toS
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java Web框架DIY Web 框架 DIY