1功能要求
功能包括:查询贴子、发表帖子、更新帖子点击量、查询某贴子详细信息(包含该贴的评论信息)、对某帖子发表评论等
详细描述:点击“正式进入”后可以查看所有已经发表的帖子,点击“某一条贴吧标题”后可进行详细信息查看,此时该条帖子的点击量要求增加1,今后每请求一次都会增加1,并能查看该条贴吧的所有信息(包含该贴吧的标题、作者、发表时间、点击量、以及对该贴的所有评论)。如点击图-2中标题为“牛妹”的贴吧后显示界面,输入评论信息(含评论作者、评论内容),点击“发表评论”,将数据存入数据库后,需跳转显示该条贴吧的所有最新信息(包含刚刚发表的新评论,评论按照评论时间进行降序排序,此时注意浏览量要增加1)
2数据库设计
3界面设计
in.jsp:
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>欢迎进入尚学堂贴吧</h2>
<br/>
<a href="main.jsp">正式进入</a>
</body>
</html>
main.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="js/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
$(function () {
/* $("#but").click(function () {
alert("aaaa");
})*/
//alert("aaaa");
$.ajax({
url:"selAllTopic",
type:"post",
success:function (data) {
var list = eval(data);
var str = "";
for(var i=0;i<list.length;i++){
str += '<tr>'
+'<td><a href="selDetail?tid='+list[i].tid+'">'+list[i].title+'</a></td>'
+'<td>'+list[i].author+'</td>'
+'<td>'+list[i].cdate+'</td>'
+'<td>'+list[i].clicknum+'</td>'
+'</tr>';
}
$("#tbody").html(str);
}
})
})
</script>
</head>
<body>
<a href="addTopic.jsp">我要发帖</a>
<br/>
<br/>
<br/>
<table>
<thead>
<tr>
<th>标题</th>
<th>发帖人</th>
<th>发帖时间</th>
<th>浏览量</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<%--<input type="button" value="测试" id="but">--%>
</body>
</html>
addTopic.jsp:
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="addTopic" method="post">
<p>
标题:<input type="text" name="title" value=""/>
</p>
<p>
发帖人:<input type="text" name="author" value=""/>
</p>
<p>
内容:<textarea rows="20" cols="50" name="content"></textarea>
</p>
<p>
<input type="submit" value="提交"/>
</p>
</form>
${mes}
</body>
</html>
detail.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>${topic.title}${topic.author}
<fmt:formatDate value="${topic.cdate}" type="both"/>
共浏览过${topic.clicknum}</h4>
<span> ${topic.content}</span>
<hr/>
<span style="color: red;font-size: 20px;">以下是评论区</span>
<c:forEach items="${repList}" var="reply">
<h4>${reply.author}
<fmt:formatDate value="${reply.cdate}" type="both"/>
</h4>
<span> ${reply.content}</span><br />
<span>================================</span>
</c:forEach>
<hr/>
<form action="addReply" method="post">
<input type="hidden" name="tid" value="${topic.tid}"/>
<p>
昵称:<input type="text" name="author" value=""/>
</p>
<p>
评论:<textarea rows="10" cols="30" name="content"></textarea>
</p>
<p>
<input type="submit" value="发表评论"/>
</p>
</form>
</body>
</html>
4代码实现
配置文件(主要配置):
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/zhang"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="typeAliasesPackage" value="an.sz.pojo"></property>
</bean>
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<property name="basePackage" value="an.sz.mapper"></property>
</bean>
<context:component-scan base-package="an.sz.service.impl"></context:component-scan>
<context:component-scan base-package="an.sz.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!--静态资源资源放行-->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
实体类的代码在此不做赘述
数据访问层的映射文件的编写:
<mapper namespace="an.sz.mapper.ReplyMapper">
<select id="selAllReply" resultType="reply">
select * from t_reply where tid=#{0} order by cdate desc
</select>
<insert id="addReply" parameterType="reply">
insert into t_reply values(default,#{tid},#{author},#{content},#{cdate})
</insert>
</mapper>
<mapper namespace="an.sz.mapper.TopicMapper">
<insert id="addTopic" parameterType="topic">
insert into t_topic values(default,#{title},#{content},#{author},#{cdate},default)
</insert>
<select id="selAllTopic" resultType="topic">
select * from t_topic order by cdate desc
</select>
<select id="selOneTopic" parameterType="int" resultType="topic">
select * from t_topic where tid=#{0}
</select>
<update id="updateNum">
update t_topic set clicknum = #{1} where tid=#{0}
</update>
</mapper>
业务层的代码编写:
@Service
public class TopicServiceImpl implements TopicService {
@Autowired
TopicMapper tMapper;
@Autowired
ReplyMapper rMapper;
@Override
public int addTopic(Topic topic) {
return tMapper.addTopic(topic);
}
@Override
public List<Topic> selAllTopic() {
List<Topic> list = tMapper.selAllTopic();
return list;
}
@Override
public Topic selOneTopic(int tid) {
Topic topic = tMapper.selOneTopic(tid);
return topic;
}
@Override
public List<Reply> selAllReply(int tid) {
List<Reply> list = rMapper.selAllReply(tid);
return list;
}
@Override
public int updateNum(int tid, int clicknum) {
return tMapper.updateNum(tid, clicknum);
}
@Override
public int addReply(Reply reply) {
return rMapper.addReply(reply);
}
}
控制层的代码编写:
@Controller
public class MyController {
@Autowired
TopicService ts;
//@RequestMapping(value = "ajax2",produces = "application/json;charset=UTF-8")
//@RequestMapping(value="addTopic",produces = "text/html;charset=utf-8")
@RequestMapping("addTopic")
public String addTopic(String title, String author, String content, HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException {
System.out.println(title+"---"+content+"---"+author);
Date cdate = new Date();
//System.out.println(cdate);
Topic topic = new Topic(title,content,author,cdate);
int num = ts.addTopic(topic);
if(num>0){
return "redirect:/main.jsp";
}else{
req.setAttribute("mes","发表帖子失败");
return "forward:/addTopic.jsp";
}
}
@RequestMapping("selAllTopic")
@ResponseBody
public void selAllTopic(HttpServletResponse resp) throws IOException {
List<Topic> list = ts.selAllTopic();
//System.out.println(list);
Gson json = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
resp.getWriter().println(json.toJson(list));
}
@RequestMapping("selDetail")
public String selOneTopic(int tid,HttpServletRequest req){
//根据tid查询topic信息
Topic top = ts.selOneTopic(tid);
int clicknum = top.getClicknum();
clicknum = clicknum+1;
ts.updateNum(tid,clicknum);
Topic topic = ts.selOneTopic(tid);
req.setAttribute("topic",topic);
//根据tid查询所有的reply信息
List<Reply> repList = ts.selAllReply(tid);
req.setAttribute("repList",repList);
return "forward:/detail.jsp";
}
@RequestMapping("addReply")
public String addReply(int tid,String author,String content){
Date cdate = new Date();
Reply reply = new Reply(tid,author,content,cdate);
int num = ts.addReply(reply);
return "forward:/selDetail?tid="+tid;
}
}