微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax和sh留言板

jsp页面


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<link rel="stylesheet"
href="${pageContext.request.contextpath }/css/mycss.css"
type="text/css"></link>




<script type="text/javascript" src="${pageContext.request.contextpath }/js/myjs.js"></script></head>


<body>
<TABLE align="center" width="60%" cellpadding="0" cellspacing="0">
<tr>
<td style="line-height: 50px; font-size: 16px; text-align: center;"
colspan="2">
雁过留声
</td>
</tr>




<s:iterator id="x" value="ar">
<tr>
<td
style="background-color: #6595d6; line-height: 25px; padding-left: 6px;">
标题:${x.NTitle } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><a href="#">回复</a></span>
</td>
</tr>
<tr>
<td>
&nbsp;&nbsp;作者:${x.NUser } &nbsp;&nbsp;&nbsp;&nbsp;日期:${x.NTime }
<br>
&nbsp;&nbsp;留言内容:
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${x.NContext }
</td>
</tr>
</s:iterator>
<tr>
<td><TABLE align="center" width="100%" cellpadding="0" cellspacing="0" id="tab"></TABLE></td>
</tr>


<tr>
<td colspan="2" align="center">&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</td>
</tr>
</TABLE>

<table align="center" width="60%" cellpadding="0" cellspacing="0">
<tr>
<td align="center">留言标题:</td>
<td>
<input type="text" name="tname" id="tname">
</td>
</tr>
<tr>
<td align="center">留言内容:</td>
<td>
<textarea rows="5" cols="50" name="tcontext" id="tcontext"></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center" >
<input type="submit" value="确定" id="btn" onclick="return lisheng();">
</td>
</tr>
</table>

</body>
</html>



ja页面


var xmlhttp;


function getIE()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
function lisheng()
{
getIE();
var title=document.getElementById("tname").value;
var context=document.getElementById("tcontext").value;
if(title.length==0&&context.length==0)
{
alert("对不起,标题内容不能为空!");
return false;
}
else
{
var url="${pageContext.request.contextpath}/note_add.action?title="+title+"&context="+context;
var myurl=encodeURI(url);
xmlhttp.open("post",myurl,true);
xmlhttp.send();
xmlhttp.onreadystatechange=getBack;
}
}
function getBack()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById("tab").innerHTML+=xmlhttp.responseText;
document.getElementById("tname").value="";
document.getElementById("tcontext").value="";

}
}


Action页面


package com.svse.action;


import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;


import com.svse.dao.NoteDAO;
import com.svse.entity.TNote;


public class NoteAction
{


private NoteDAO dao=new NoteDAO();
private List ar;
private String title;
private String context;



public String getTitle()
{
return title;
}




public void setTitle(String title)
{
this.title = title;
}




public String getContext()
{
return context;
}




public void setContext(String context)
{
this.context = context;
}




public List getAr()
{
return ar;
}




public void setAr(List ar)
{
this.ar = ar;
}




//全查询
public String all()
{
this.ar=this.dao.getAllNote();
return "myall";
}
//增加
public String add()
{
try
{
String newtitle=new String(title.getBytes("ISO8859_1"),"utf-8");
String newcontext=new String(context.getBytes("ISO8859_1"),"utf-8");
SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd hh:ss:mm");
String time=dd.format(new Date());


TNote note=new TNote();
note.setNContext(newcontext);
note.setNTitle(newtitle);
note.setNUser("你大爷");
note.setNTime(time);
//第一步:增加
TNote tt=this.dao.addNote(note);
//第二步:出去
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out = response.getWriter();
StringBuffer sb=new StringBuffer();


sb.append("<tr><td style='background-color: #6595d6; line-height: 25px; padding-left: 6px;'>标题:"+tt.getNTitle()+" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><a href='#'>回复</a></span></td></tr><tr><td>&nbsp;&nbsp;作者:"+tt.getNUser()+" &nbsp;&nbsp;&nbsp;&nbsp;日期:"+tt.getNTime()+"<br>&nbsp;&nbsp;留言内容:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+tt.getNContext()+"</td></tr>");
out.print(sb);

out.flush();
out.close();




}
catch (Exception e)
{
e.printstacktrace();
}

return null;
}
}



Dao页面


//全查询 public List getAllNote() { List ar=this.getSesison().createquery("from TNote").list(); this.closeAll(); return ar; } //增加 public TNote addNote(TNote note) { TNote tt=new TNote(); this.beginTransaction(); this.getSesison().save(note); //这个地方能不能处到刚刚增加的这个对象的主键呢? int t=note.getNId(); tt.setNContext(note.getNContext()); tt.setNId(t);//这是你刚刚增加这第记录的主键 tt.setNTime(note.getNTime()); tt.setNTitle(note.getNTitle()); tt.setNUser(note.getNUser()); this.commitTransaction(); return tt; }

原文地址:https://www.jb51.cc/ajax/161349.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐