DuestionsAction 控制类
package cn.action;
import java.util.Date;
import java.util.List;
import cn.biz.DuestionsBiz;
import cn.entity.Answers;
import cn.entity.Duestions;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class DuestionsAction extends ActionSupport {
private DuestionsBiz duestionsBiz;
private List<Duestions> duestions;
private List<Answers> answers;
private Answers answer;
private Duestions duestion;
private Integer id;
/**
* 显示列表
* @return
* @throws Exception
*/
public String getAll() throws Exception{
/*
* 如果 id 不为空,代表是从 findDetatil.acton 传来的数据
* 第一步:根据 id 获取问题表的数据
* 第二步:插入应答表的数据,主键是问题表
* 第三步:设置问题表的应答次数,和最新的时间
* 第四步:修改问题表数据
*/
if(id != null){
duestion = duestionsBiz.findById(id);
duestionsBiz.insert(new Answers(duestion, answer.getAnscontent(), new Date()));
duestion.setAnsewercount(duestionsBiz.findAnswersList(id).size());
duestion.setLastmodidfied(new Date());
duestionsBiz.update(duestion);
id = null;//清空,要不然重复插入应答表的数据
}
//最后,显示列表
duestions = duestionsBiz.findAll();
return SUCCESS;
}
/**
* 应答表列表
* @return
* @throws Exception
*/
public String findDetatil() throws Exception{
//传过的 id 来获取数据
if(id != null){
duestion = duestionsBiz.findById(id);
answers = duestionsBiz.findAnswersList(id);
}
return SUCCESS;
}
/**
* 插入问题表数据
* @return
* @throws Exception
*/
public String doAdd() throws Exception{
if(duestion!=null){
duestionsBiz.add(new Duestions(duestion.getTitle(), duestion.getDetaildesc(), 0, new Date()));
}
return SUCCESS;
}
public DuestionsBiz getDuestionsBiz() {
return duestionsBiz;
}
public void setDuestionsBiz(DuestionsBiz duestionsBiz) {
this.duestionsBiz = duestionsBiz;
}
public List<Duestions> getDuestions() {
return duestions;
}
public void setDuestions(List<Duestions> duestions) {
this.duestions = duestions;
}
public Duestions getDuestion() {
return duestion;
}
public void setDuestion(Duestions duestion) {
this.duestion = duestion;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Answers> getAnswers() {
return answers;
}
public void setAnswers(List<Answers> answers) {
this.answers = answers;
}
public Answers getAnswer() {
return answer;
}
public void setAnswer(Answers answer) {
this.answer = answer;
}
}
struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<package name="cn.action" extends="struts-default" namespace="/">
<!-- 显示列表 -->
<action name="getAll" class="cn.action.DuestionsAction" method="getAll">
<result name="success">/list.jsp</result>
</action>
<!-- 查看应答 -->
<action name="findDetatil" class="cn.action.DuestionsAction"
method="findDetatil">
<result name="success">/detail.jsp</result>
</action>
<!-- 添加问题 -->
<action name="doAdd" class="cn.action.DuestionsAction" method="doAdd">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
index.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<!-- 跳转页面 -->
<c:redirect url="getAll.action" />
</body>
</html>
list.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>在线回答</title>
<style type="text/css">
*{ margin:0; padding:0; }
body{ font-family:"宋体"; color:#333; font-size:14px; background:#fff;}
a{ color:#069; text-decoration:underline;}
a:hover{ color:#f60; text-decoration:underline;}
h3{ text-align:center; line-height:40px;}
p{ text-align: center; height: 23px;}
table{ margin:0 auto; line-height:23px;}
table tr th{ background:#B8860B;}
table tr td{ padding:0 8px;}
table .tdBg td{ background:#999}
</style>
</head>
<body>
<div style=" padding: 8px; width: 480px; margin: 0 auto;">
<h3>在线回答</h3>
<p>
<a href="question.jsp">我要提问</a>
</p>
<table border="1">
<tr>
<th>序号</th>
<th>问题</th>
<th>回答次数</th>
<th>最后修改</th>
</tr>
<s:iterator value="duestions" status="st">
<tr <s:if test="#st.even">class="tdBg"</s:if>>
<td><s:property value="id"></s:property></td>
<td><a href="findDetatil.action?id=<s:property value='id'/>">
<s:property value="title"></s:property></a></td>
<td><s:property value="ansewercount"></s:property></td>
<td> <s:date name="lastmodidfied" format="yyyy-MM-dd"></s:date></td>
</tr>
</s:iterator>
</table>
</div>
</body>
</html>
效果图:
detail.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>我来回答</title>
<style type="text/css">
*{ margin:0; padding:0; }
body{ font-family:"宋体"; color:#333; font-size:14px; background:#fff;}
table{ margin:0 auto; line-height:23px;}
table tr td{ padding:0 8px;}
table tr .tdbg{ background: #999;}
</style>
</head>
<body>
<div style=" padding: 8px; width: 480px; margin: 0 auto;">
<form action="getAll.action" method="post">
<table border="1">
<tr><td>问题:</td><td class="tdbg">
<s:property value="duestion.title"></s:property></td></tr>
<tr><td>问题描述:</td><td class="tdbg">
<s:property value="duestion.detaildesc"></s:property></td></tr>
<s:iterator value="answers">
<tr><td>网友回答</td><td class="tdbg">
<s:property value="anscontent"></s:property>
<br><s:date name="ansdate" format="yyyy-MM-dd"></s:date>
</td></tr>
</s:iterator>
<tr><td>我来回答</td><td>
<input type="hidden" name="id" value="<s:property value='id'/>">
<input type="text" name="answer.anscontent"></td></tr>
<tr><td><a href="index.jsp">返回</a></td><td><input type="submit"></td></tr>
</table>
</form>
</div>
</body>
</html>
效果图:
question.jsp 页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>我要提问</title>
<style type="text/css">
body{ font-family:"宋体"; color:#333; font-size:14px; background:#fff;}
a{ color:#069; text-decoration:underline;}
a:hover{ color:#f60; text-decoration:underline;}
h3{ text-align:center; line-height:40px;}
p{ text-align: center;}
</style>
</head>
<body>
<div style=" width: 300px; margin: 0 auto;">
<form action="doAdd.action" method="post">
<h3>我要提问</h3>
<p><a href="index.jsp">返回首页</a></p>
<table>
<tr><td>问题:</td><td><input type="text" name="duestion.title"></td></tr>
<tr><td>问题描述:</td><td><input type="text" name="duestion.detaildesc"></td></tr>
<tr><td></td><td><input type="submit" value="保存问题">
<a href="index.jsp">返回</a></td></tr>
</table>
</form>
</div>
</body>
</html>
效果图: