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" οnclick="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;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值