Ajax:异步的javaScript和XML,也就是不提交页面,也不转发请求或是重定向,仅仅在当前页面作一些改变就会
反映到数据库里面,当前页面根本不提交就可以!
增加讨论区按钮是button类型,里面的onclick可以执行如下的javaScript函数:
function addSection()
{
window.open("addSection.jsp","_blank","width=200,height=400")
}
addSection.jsp:
插入section表和建立该板块文章的保存表sectionTable是由自动生成的,当然还有时间日期也是自动生成的
<link rel="stylesheet" type="text/css" href="css/editCss.css">
<script language="javascript">
function addSection()
{
fom = document.forms[0] ;
sectionName = fom.sectionName.value ;
sectionEngName = fom.sectionEnglishName.value ;
if(sectionName==null||sectionName.length==0)
{
alert("讨论区名称不可以为空!");
}
else if(sectionEngName==null||sectionEngName.length==0)
{
alert("讨论区英文名称不可以为空的!"); //这里应该校验英文名称中不能有中文字符!
}
else
{
fom.action="addSectionServlet" ; //提交给Servlet,让它去写数据库!
fom.submit() ;
self.close();
self.opener.location.reload() ;
}
}
</script>
<body>
<table class="tb_title">
<tr>
<td>增加讨论区</td>
</tr>
</table>
<form>
<table class="tbMain">
<tr>
<td class="td_name">讨论区名称</td>
<td class="td_value"><input type="text" name="sectionName" /></td>
</tr>
<tr>
<td class="td_name">讨论区英文名称</td>
<td class="td_value"><input type="text" name="sectionEnglishName" /></td>
</tr>
<tr>
<td class="td_name"> </td>
<td class="td_value" >
<input type="button" value="增加" class="BUTTON_01" οnclick="addSection()"/>
//点击onclick之后这个新开的增加版面的窗口必须关闭,然后index页面中的sectionList还必须要刷新
//才能多一个版面
<input type="reset" value="重置" class="BUTTON_01" />
</td>
</tr>
</table>
</form>
</body>
===========================
新建一个Servlet叫做AddSectionServlet:
request.setCharacterEncoding("GBK");
String sectionName = request.getParameter("sectionName") ;
String sectionEnglish = request.getParameter("sectionEnglishName") ;
SectionBean bean = new SectionBean() ;
bean.setSectionName(sectionName) ;
bean.setSectionEnglishName(sectionEnglishName) ;
//假如英文名字叫做java_tech的话,那么表应该叫做mybbs_java_tech_article
String[] str = sectionEnglishName.split(" ");
StringBuffer sectionTable = new StringBuffer("mybbs_");
for(int i = 0 ; i < str.length ; i ++)
{
sectionTable.append(str[i]).append("_");
}
sectionTable.append("articles");
bean.setSectionTable(sectionTable.stoString());
SectionService service = new SectionService() ;
service.setSection(bean) ;
service.insert() ; //插入结束。
request.getRequestDispatcher("sectionList.jsp").forward(request,response) ;
====================================================
下午课程开始:
Servle中要保证是特定的jsp提交过来的才可以访问,否则就弹回到开始页面!
String referer = request.getHeader("Referer");
if(referer==null && !refer.endsWith("addSection.jsp"))
{
request.getRequestDispatchcer("index.jsp").forward(request,response);
return ;
}
还要保证一点,在增添讨论区之前首先要在数据库中建立讨论区的文章表!!!!
加上一个contentList.jsp页面,点击每一个版块区都会进入到这个页面来浏览文章的内容!
这个页面中要显示:编号、标题、发表日期、作者
为了显示讨论区文章中的内容,在逻辑层中要建立一个ArticleService:
数据层中还要建立一个ArticleBean
public class ArticleBean
{
private Integer id ;
private String title ;
private String content ;
private Timestamp publishDate ;
private Integer authorId ;
private Integer visitAmount ;
private Integer parentId ; //父标题,如果有这个parentId,则这篇文章属于回复帖,否则是主帖!
private String mark ; //好文章要标记下来!
}
public class ArticleService
{
public static ArrayList getAllArticles(String table) throws SQLException
{
String sql = "select * from " + table + " where parentId is null order by publishDate desc" ;
return DBManager.getInstance().getDBReader().getData(sql,ArticleBean.class);
}
public static ArrayList getAllArticlesById(String table , String id) throws SQLException
{
String sql = "select * from " + table + " where id=" + id + " or parentId=" + id
+ " order by publishDate";
return DBManager.getInstance().getDBReader().getData(sql,ArticleBean.class);
}
}
contentList.jsp:
<%
SectionBean bean = (SectionBean)((Map)application.getAttribute("sections")).get(new Integer(request.getParameter("sectionId"))) ;
pageContext.setAttribute("articles",ArticleService.getAllArticles(bean.getSectionTable()));
//这里需要提供版块的文章存储表……可以考虑把所有的表存在一个应用程序作用域中……
//这里考虑加入一个监听器……
%>
package org.mybbs.listener ;
public class MybbsApplicationListener implements ServletContextListener
{
public void contextDestroyed(ServletContextEvent arg0)
{}
public void contextInitialized(ServletContextEvent arg0)
{
ArrayList sections = SectionService.getAllSection();
//section里面存的是版块SectionBean的集合
Map map = new HashMap();
Iterator it = sections.iterator() ;
while(it.hasNext())
{
SectionBean bean = (SectionBean)it.next() ;
map.put(bean.getSectionId(),bean);
}
arg0.getServletContext().setAttribute(Constant.SECTION_ATTR_NAME,map);
}
}
<listener>
<listener-class></listener-class>
</listener>
org.mybbs.util :装常量类的包
public class Constant
{
public static final String SECTION_ATTR_NAME = "org.mybbs.util.constant.section" ;
}
为了显示每个主帖的内容,必须还得再创建一个叫做viewContent.jsp的页面:
<%
%>