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>
效果图:
相关推荐
总结来说,`struts.xml` 负责Struts2的Action配置,`applicationContext.xml` 管理Spring的Bean和依赖,而`web.xml` 定义了Web应用的基本结构和组件。这三个文件共同协作,构建了一个功能完善的Java Web应用,实现了...
1. **struts.xml**: 这是Struts 2框架的核心配置文件,定义了Action类、结果类型、拦截器等关键元素。 2. **Action类**: 代表业务逻辑,通常会处理HTTP请求,调用服务层方法,并将结果转发到特定的结果视图。 3. *...
Struts配置文件(struts-config.xml)定义了Action和ActionForm,以及它们与视图之间的映射,使得代码结构清晰,易于维护。 **Hibernate框架** Hibernate是一个流行的ORM框架,它允许开发者用Java对象来表示数据库...
总结起来,SSH集成代理2.0版与Struts.xml中的DelegatingActionProxy代理搭配,提供了一种高效、灵活的MVC框架解决方案,允许开发者在不改动业务逻辑代码的情况下,通过配置文件实现对Action的各种增强功能。...
Struts2通过Action类和配置文件定义了业务逻辑和视图之间的映射,同时支持多种结果类型,如JSP、FreeMarker、Velocity等。 Spring框架则是一个全面的后端解决方案,它包含了依赖注入(DI)、面向切面编程(AOP)、...
总的来说,SSH集成代理2.0版与Struts.xml中控制器的搭配,为Java Web开发提供了一套强大且优化的解决方案。通过理解这些框架的核心原理和整合方式,开发者能够更好地构建高效、可扩展的企业级应用程序。
【标题】"jsp136酒店管理系统ssh+mysql.zip"是一个包含Java Web应用程序的压缩包,主要用于实现酒店管理系统的功能。SSH在这里指的是Spring、Struts和Hibernate三个开源框架的组合,它们是Java开发中常见的MVC...
**项目结构**:一个标准的SSH项目通常会包含多个模块,如Web层(包括JSP页面和Struts2配置)、控制层(Struts2 Action)、服务层(Spring Service)、持久层(Hibernate Entity和DAO)以及配置文件(如struts.xml、...
5. **响应**:Struts 2根据Action执行的结果,决定跳转到哪个JSP页面或者返回JSON等格式的数据。 这个简单的SSH整合案例有助于理解它们各自的功能以及如何相互配合。在实际项目中,SSH框架可以处理更复杂的业务逻辑...
《深入解析JSP SSH项目:基于Struts、Spring和Hibernate的移动应用开发》 SSH(Struts、Spring、Hibernate)是Java开发中的经典MVC框架组合,尤其在企业级应用中广泛使用。在这个名为“移动ssh项目”的源码中,我们...
然后,定义Action类来处理业务逻辑,创建Struts2配置文件(struts.xml)来映射URL请求和Action。最后,制作JSP视图页面,用于用户交互。 2. **Spring**:Spring框架的核心在于依赖注入(DI)和面向切面编程(AOP)。...
在`struts.xml`中配置`DelegatingActionProxy`,可以方便地对Action进行动态代理,添加拦截器以实现诸如事务控制、权限验证等功能,同时结合SSH集成代理1.2版的特定搭配,可以更好地适应项目的需求并优化整体架构。
在Struts2中,一个核心的配置文件是struts.xml,用于配置action映射,包含namespace、action、result等元素。它使用拦截器(interceptor)机制来管理请求的流程,处理输入验证、文件上传等功能。 1. 核心概念: - ...
在这个SSH项目中,你可能会看到诸如Action类、配置文件(如struts.xml、spring.xml、hibernate.cfg.xml)、实体类(Entity)、DAO(数据访问对象)接口和服务类(Service)等不同组成部分。每个部分都有其特定的职责...
**sshdemo**:这个压缩包中的sshdemo项目可能是包含了一个简单的SSH整合示例,可能包括了web.xml、struts.xml、spring配置文件、hibernate配置文件、DAO、Service、Controller层的Java类,以及相关的HTML和JSP页面。...
它通过Action类处理用户请求,并使用配置文件(struts.xml)来定义这些Action的映射。 2. Spring:Spring框架提供了一种强大的依赖注入机制,使得对象之间的依赖关系可以被外部化,增强了代码的可测试性和可维护性...
SSH项目是一种经典的Java Web开发框架组合,由Spring、Hibernate和Struts2三个开源框架组成。这个项目示例提供了一个基于这些技术的简单应用,帮助开发者理解如何将它们整合在一起进行实际开发。 **Spring框架**是...