下面就简单学习下Struts2+Hibernate的整合。
其实就七个步骤:
第一步:添加所需的jar包。
第二步:创建实体类以及对应的映射文件。
第三步:配置hibernate.cfg.xml配置文件。
第四步:配置struts.xml配置文件。
第五步:创建相应的action。
第六步:在web.xml中配置启动Struts2框架的过滤器。
第七步:编写JSP显示页面。
---------------------------------------------------------------------------------------------------------------------------
具体细节及代码如下(代码仅供参考):
第一步:jar包的添加这里不多说。
第二步:
实体类Message.java
/**
* ClassName: Message.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.domain;
import java.io.Serializable;
import java.util.Date;
/**
* 留言实体类
*
* @author yjd
*/
public class Message implements Serializable {
private static final long serialVersionUID = 6617253915349620625L;
private Integer id; // OID
private String nickname; // 昵称
private boolean gender; // 性别
private String header_img; // 头像
private String qq; // qq
private String email; // 邮箱号
private String content; // 留言内容
private String ip; // 发布者的IP地址
private Date pubTime; // 留言时间
private String reply;// 管理员回复内容
private Date replyTime; // 回复时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public String getHeader_img() {
return header_img;
}
public void setHeader_img(String headerImg) {
header_img = headerImg;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Date getPubTime() {
return pubTime;
}
public void setPubTime(Date pubTime) {
this.pubTime = pubTime;
}
public String getReply() {
return reply;
}
public void setReply(String reply) {
this.reply = reply;
}
public Date getReplyTime() {
return replyTime;
}
public void setReplyTime(Date replyTime) {
this.replyTime = replyTime;
}
@Override
public String toString() {
return "Message [content=" + content + ", email=" + email + ", gender="
+ gender + ", header_img=" + header_img + ", id=" + id
+ ", ip=" + ip + ", nickname=" + nickname + ", pubTime="
+ pubTime + ", qq=" + qq + "]";
}
}
实体类映射文件Message.hbm.xml.
[html] view plain copy print ?
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
-
- <hibernate-mapping>
- <class name="com.yjd.nb.domain.Message" table="msg">
- <id name="id" column="id">
- <generator class="native" />
- </id>
-
- <property name="nickname" />
- <property name="gender" />
- <property name="header_img" />
- <property name="qq" />
- <property name="email" />
- <property name="ip" />
- <property name="content" />
- <property name="pubTime" column="pub_time" />
- <property name="reply" />
- <property name="replyTime" column="reply_time" />
- </class>
- </hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yjd.nb.domain.Message" table="msg">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="nickname" />
<property name="gender" />
<property name="header_img" />
<property name="qq" />
<property name="email" />
<property name="ip" />
<property name="content" />
<property name="pubTime" column="pub_time" />
<property name="reply" />
<property name="replyTime" column="reply_time" />
</class>
</hibernate-mapping>
第三步:Hibernate配置文件hibernate.cfg.xml.
[html] view plain copy print ?
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
- <session-factory>
-
- <property name="hibernate.connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="hibernate.connection.url">
- jdbc:mysql:///s2h-model
- </property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
-
-
- <property name="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
-
-
- <property name="connection.provider_class">
- org.hibernate.connection.C3P0ConnectionProvider
- </property>
- <property name="hibernate.c3p0.min_size">5</property>
- <property name="hibernate.c3p0.max_size">30</property>
- <property name="hibernate.c3p0.timeout">1800</property>
- <property name="hibernate.c3p0.max_statements">50</property>
-
-
- <property name="hbm2ddl.auto">update</property>
- <property name="show_sql">true</property>
-
-
-
- <mapping resource="com/yjd/nb/domain/Message.hbm.xml" />
-
- </session-factory>
- </hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库的连接参数配置 -->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql:///s2h-model
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- c3p0连接池的配置 -->
<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<!-- 其它配置 -->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<!-- <property name="format_sql">true</property> -->
<!-- 注册映射文件 -->
<mapping resource="com/yjd/nb/domain/Message.hbm.xml" />
</session-factory>
</hibernate-configuration>
第四步:Struts2配置文件struts.xml.
[html] view plain copy print ?
- <?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>
- <!--
- 在struts.xml文件中可以使用<constant>标记来对default.properties中的常量属性做覆盖性配置,最常更改的几个常量属性是:
- 1.struts.i18n.encoding=UTF-8:请求消息的编码方式
- 2.struts.action.extension=action:指定被struts2处理的请求后缀类型。多个用逗号隔开。如:action,do,go
- 3.struts.configuration.xml.reload=false:当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开
- 4.struts.devMode=false:是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开
- 5.struts.serve.static.browserCache=true:设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭。
- 6.struts.objectFactory=spring:指定由spring负责action对象的创建
- 7.struts.enable.SlashesInActionNames=false:启用Action的name是否支持斜线(/)
- 8.struts.enable.DynamicMethodInvocation=false:启用动态方法调用
- 9.struts.ui.theme=simple:UI主题类型
- -->
- <constant name="struts.action.extension" value="action,do,htm" />
- <constant name="struts.configuration.xml.reload" value="true" />
- <constant name="struts.devMode" value="false" />
- <constant name="struts.serve.static.browserCache" value="false" />
-
-
- <!--
- package常用的属性:
- 1.name:包名,作为被其它包引用的标识。必须的。实际应用中,应该把一组业务功能相关的Action放在同一个包下。
- 2.extends:指定要扩展的包名。一般会继承自struts-default包。struts-default包是struts2内置的,它定义了struts2内部的众多拦截器和Result类型。
- 3.namespace:指定名称空间。Struts2中Action的请求URI映射由namespace和action名称两部分组成。如果不指定该属性,默认的命名空间为""(空字符串)。
- 4.abstract:声明包为抽象的。抽象包中不能配置action。
- -->
- <package name="front" namespace="/" extends="struts-default">
- <action name="index" class="com.yjd.nb.web.action.IndexAction">
- <result>/main.jsp</result>
- </action>
- </package>
- <package name="front.msgOpt" namespace="/msg" extends="struts-default">
- <action name="add" class="com.yjd.nb.web.action.MsgAction"
- method="add">
- <result name="list" type="redirect">/index.action</result>
- </action>
- </package>
- </struts>
<?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>
<!--
在struts.xml文件中可以使用<constant>标记来对default.properties中的常量属性做覆盖性配置,最常更改的几个常量属性是:
1.struts.i18n.encoding=UTF-8:请求消息的编码方式
2.struts.action.extension=action:指定被struts2处理的请求后缀类型。多个用逗号隔开。如:action,do,go
3.struts.configuration.xml.reload=false:当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开
4.struts.devMode=false:是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开
5.struts.serve.static.browserCache=true:设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭。
6.struts.objectFactory=spring:指定由spring负责action对象的创建
7.struts.enable.SlashesInActionNames=false:启用Action的name是否支持斜线(/)
8.struts.enable.DynamicMethodInvocation=false:启用动态方法调用
9.struts.ui.theme=simple:UI主题类型
-->
<constant name="struts.action.extension" value="action,do,htm" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.serve.static.browserCache" value="false" />
<!-- Struts2以package形式管理Action的配置 -->
<!--
package常用的属性:
1.name:包名,作为被其它包引用的标识。必须的。实际应用中,应该把一组业务功能相关的Action放在同一个包下。
2.extends:指定要扩展的包名。一般会继承自struts-default包。struts-default包是struts2内置的,它定义了struts2内部的众多拦截器和Result类型。
3.namespace:指定名称空间。Struts2中Action的请求URI映射由namespace和action名称两部分组成。如果不指定该属性,默认的命名空间为""(空字符串)。
4.abstract:声明包为抽象的。抽象包中不能配置action。
-->
<package name="front" namespace="/" extends="struts-default">
<action name="index" class="com.yjd.nb.web.action.IndexAction">
<result>/main.jsp</result>
</action>
</package>
<package name="front.msgOpt" namespace="/msg" extends="struts-default">
<action name="add" class="com.yjd.nb.web.action.MsgAction"
method="add">
<result name="list" type="redirect">/index.action</result>
</action>
</package>
</struts>
第五步:创建action类。
跳转到首页的action:
[java] view plain copy print ?
-
-
-
-
-
-
-
- package com.yjd.nb.web.action;
-
- import org.apache.struts2.ServletActionContext;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.yjd.domain.common.PageModel;
- import com.yjd.nb.domain.Message;
- import com.yjd.nb.service.MessageService;
-
-
-
-
-
-
- public class IndexAction extends ActionSupport {
- private static final long serialVersionUID = -3634083788212536690L;
- private MessageService messageService = new MessageService();
- private int pageNo = 1;
- private int pageSize = 10;
-
- public String execute() {
- PageModel<Message> pm = messageService.findByPage(pageNo, pageSize);
-
- ServletActionContext.getRequest().setAttribute("pm", pm);
-
- return SUCCESS;
- }
-
- public MessageService getMessageService() {
- return messageService;
- }
-
- public void setMessageService(MessageService messageService) {
- this.messageService = messageService;
- }
-
- public int getPageNo() {
- return pageNo;
- }
-
- public void setPageNo(int pageNo) {
- this.pageNo = pageNo;
- }
-
- public int getPageSize() {
- return pageSize;
- }
-
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- }
/**
* ClassName: IndexAction.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.web.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yjd.domain.common.PageModel;
import com.yjd.nb.domain.Message;
import com.yjd.nb.service.MessageService;
/**
* 首页的请求处理类
*
* @author yjd
*/
public class IndexAction extends ActionSupport {
private static final long serialVersionUID = -3634083788212536690L;
private MessageService messageService = new MessageService();
private int pageNo = 1;
private int pageSize = 10;
public String execute() {
PageModel<Message> pm = messageService.findByPage(pageNo, pageSize);
ServletActionContext.getRequest().setAttribute("pm", pm);
// ActionContext.getContext().put("pm", pm);
return SUCCESS;
}
public MessageService getMessageService() {
return messageService;
}
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
实体操作action:
[java] view plain copy print ?
-
-
-
-
-
-
-
- package com.yjd.nb.web.action;
-
- import java.util.Date;
-
- import org.apache.struts2.ServletActionContext;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.yjd.nb.domain.Message;
- import com.yjd.nb.service.MessageService;
-
-
-
-
-
-
- public class MsgAction extends ActionSupport {
- private static final long serialVersionUID = -5736885633645314440L;
- private MessageService messageService = new MessageService();
- private Message msg;
-
-
- public String add() {
- msg.setIp(ServletActionContext.getRequest().getRemoteAddr());
- msg.setPubTime(new Date());
-
- messageService.add(msg);
- return "list";
- }
-
- public Message getMsg() {
- return msg;
- }
-
- public void setMsg(Message msg) {
- this.msg = msg;
- }
-
- }
/**
* ClassName: MsgAction.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.web.action;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yjd.nb.domain.Message;
import com.yjd.nb.service.MessageService;
/**
* 前台针对Msg操作的处理类
*
* @author yjd
*/
public class MsgAction extends ActionSupport {
private static final long serialVersionUID = -5736885633645314440L;
private MessageService messageService = new MessageService();
private Message msg;
// 添加实体
public String add() {
msg.setIp(ServletActionContext.getRequest().getRemoteAddr());
msg.setPubTime(new Date());
messageService.add(msg);
return "list";
}
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
}
第六步:在web.xml中配置启动Struts2框架的过滤器。
[html] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第七步:编写JSP显示页面。
首页跳转index.jsp
[html] view plain copy print ?
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%--<jsp:forward page="index.action"/> --%>
- <meta http-equiv="refresh" content="0;url=index.action" />
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%--<jsp:forward page="index.action"/> --%>
<meta http-equiv="refresh" content="0;url=index.action" />
主页:main.jsp
[html] view plain copy print ?
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <%@taglib uri="http://hi.csdn.net/tjcyjd/tags" prefix="myTag" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html><head><title>在线留言系统</title>
- <meta http-equiv=content-type content="text/html; charset=UTF-8">
- <link href="images/style.css" type=text/css rel=stylesheet></link>
- <style type="text/css">
- body{font-size: 12px}
- table{font-size: 12px}
- a{font-size:12px}
- .current{font-size:12px;}
- </style>
-
- </head>
- <body>
- <form name=form1 action="msg/add.action" method=post>
- <table class=tab cellspacing=1 align=center border=0>
- <tbody>
- <tr>
- <td class=title background=images/titlebg.jpg colspan=2
- height=25><span>请 签 写 留 言</span> </td></tr>
- <tr>
- <td align=right width="20%">您的称呼: </td>
- <td width="80%"><input id="username" maxlength=50 name="msg.nickname"/> </td></tr>
- <tr>
- <td align=right width="20%">您的性别: </td>
- <td width="80%"><input id=sex type=radio value="true" name="msg.gender">
- 男 <input type=radio checked value="false" name="msg.gender"> 女
- </td></tr>
- <tr>
- <td align=right>选择头像: </td>
- <td><select id=image name="msg.header_img"> <option
- value=1.gif selected>1.gif</option> <option
- value=2.gif>2.gif</option> <option
- value=3.gif>3.gif</option> <option
- value=4.gif>4.gif</option> <option
- value=5.gif>5.gif</option> <option
- value=6.gif>6.gif</option> <option
- value=7.gif>7.gif</option> <option
- value=8.gif>8.gif</option> <option
- value=9.gif>9.gif</option> <option
- value=10.gif>10.gif</option> <option
- value=11.gif>11.gif</option> <option
- value=12.gif>12.gif</option> <option
- value=13.gif>13.gif</option> <option
- value=14.gif>14.gif</option> <option
- value=15.gif>15.gif</option> <option
- value=16.gif>16.gif</option> <option
- value=17.gif>17.gif</option> <option
- value=18.gif>18.gif</option> <option
- value=19.gif>19.gif</option> <option
- value=20.gif>20.gif</option></select> </td></tr>
- <tr>
- <td align=right>您的qq: </td>
- <td><input id=qq maxlength=50 name="msg.qq"> </td></tr>
- <tr>
- <td align=right>您的邮箱: </td>
- <td><input id=email maxlength=50 name="msg.email"> </td></tr>
- <tr>
- <td align=right>留言内容: </td>
- <td><textarea id=content name="msg.content" rows=5 cols=50></textarea>
- </td></tr>
- <tr>
- <td align="center" colspan=2><input type=submit value=提交>
- <input type=reset value=重置> </td></tr></tbody></table></form>
- <table class=tab cellspacing=1 align=center border=0>
- <tbody>
- <tr>
- <td class=title background=images/titlebg.jpg colspan=2
- height=25><span>留 言 列 表【<a href="login.jsp">管理登录</a>】 </span></td></tr>
-
- <c:forEach items="${pm.data}" var="msg">
- <tr>
- <td align="center" width="20%" rowspan=4>你好 : ${msg.nickname} ${msg.gender ? "帅哥" : "靓妹"} <br><br><img
- src="images/${msg.header_img}"> </td></tr>
- <tr>
- <td width="80%" height="100%">发表于: <fmt:formatDate value="${msg.pubTime}" pattern="yyyy-MM-dd HH:mm:ss"/> <img
- src="images/8_online.gif" border=0> <a href="mailto:${msg.email}"><img
- src="images/email.gif" border=0></a> <img src="images/ip.gif"
- border=0> 来自:${msg.ip}</td></tr>
- <tr>
- <td>${msg.content}</td></tr>
- <tr>
- <td><font color=#ff0000>管理员回复:</font> </td></tr>
- </c:forEach>
-
- </tbody></table>
-
- <myTag:pager pageSize="${pm.pageSize}" pageNo="${pm.pageNo}" url="index.action" recordCount="${pm.recordCount}"/>
-
- </body></html>
下面就简单学习下Struts2+Hibernate的整合。
其实就七个步骤:
第一步:添加所需的jar包。
第二步:创建实体类以及对应的映射文件。
第三步:配置hibernate.cfg.xml配置文件。
第四步:配置struts.xml配置文件。
第五步:创建相应的action。
第六步:在web.xml中配置启动Struts2框架的过滤器。
第七步:编写JSP显示页面。
---------------------------------------------------------------------------------------------------------------------------
具体细节及代码如下(代码仅供参考):
第一步:jar包的添加这里不多说。
第二步:
实体类Message.java
/**
* ClassName: Message.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.domain;
import java.io.Serializable;
import java.util.Date;
/**
* 留言实体类
*
* @author yjd
*/
public class Message implements Serializable {
private static final long serialVersionUID = 6617253915349620625L;
private Integer id; // OID
private String nickname; // 昵称
private boolean gender; // 性别
private String header_img; // 头像
private String qq; // qq
private String email; // 邮箱号
private String content; // 留言内容
private String ip; // 发布者的IP地址
private Date pubTime; // 留言时间
private String reply;// 管理员回复内容
private Date replyTime; // 回复时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public String getHeader_img() {
return header_img;
}
public void setHeader_img(String headerImg) {
header_img = headerImg;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Date getPubTime() {
return pubTime;
}
public void setPubTime(Date pubTime) {
this.pubTime = pubTime;
}
public String getReply() {
return reply;
}
public void setReply(String reply) {
this.reply = reply;
}
public Date getReplyTime() {
return replyTime;
}
public void setReplyTime(Date replyTime) {
this.replyTime = replyTime;
}
@Override
public String toString() {
return "Message [content=" + content + ", email=" + email + ", gender="
+ gender + ", header_img=" + header_img + ", id=" + id
+ ", ip=" + ip + ", nickname=" + nickname + ", pubTime="
+ pubTime + ", qq=" + qq + "]";
}
}
实体类映射文件Message.hbm.xml.
[html] view plain copy print ?
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
-
- <hibernate-mapping>
- <class name="com.yjd.nb.domain.Message" table="msg">
- <id name="id" column="id">
- <generator class="native" />
- </id>
-
- <property name="nickname" />
- <property name="gender" />
- <property name="header_img" />
- <property name="qq" />
- <property name="email" />
- <property name="ip" />
- <property name="content" />
- <property name="pubTime" column="pub_time" />
- <property name="reply" />
- <property name="replyTime" column="reply_time" />
- </class>
- </hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yjd.nb.domain.Message" table="msg">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="nickname" />
<property name="gender" />
<property name="header_img" />
<property name="qq" />
<property name="email" />
<property name="ip" />
<property name="content" />
<property name="pubTime" column="pub_time" />
<property name="reply" />
<property name="replyTime" column="reply_time" />
</class>
</hibernate-mapping>
第三步:Hibernate配置文件hibernate.cfg.xml.
[html] view plain copy print ?
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
- <session-factory>
-
- <property name="hibernate.connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="hibernate.connection.url">
- jdbc:mysql:///s2h-model
- </property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
-
-
- <property name="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
-
-
- <property name="connection.provider_class">
- org.hibernate.connection.C3P0ConnectionProvider
- </property>
- <property name="hibernate.c3p0.min_size">5</property>
- <property name="hibernate.c3p0.max_size">30</property>
- <property name="hibernate.c3p0.timeout">1800</property>
- <property name="hibernate.c3p0.max_statements">50</property>
-
-
- <property name="hbm2ddl.auto">update</property>
- <property name="show_sql">true</property>
-
-
-
- <mapping resource="com/yjd/nb/domain/Message.hbm.xml" />
-
- </session-factory>
- </hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库的连接参数配置 -->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql:///s2h-model
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- c3p0连接池的配置 -->
<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<!-- 其它配置 -->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<!-- <property name="format_sql">true</property> -->
<!-- 注册映射文件 -->
<mapping resource="com/yjd/nb/domain/Message.hbm.xml" />
</session-factory>
</hibernate-configuration>
第四步:Struts2配置文件struts.xml.
[html] view plain copy print ?
- <?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>
- <!--
- 在struts.xml文件中可以使用<constant>标记来对default.properties中的常量属性做覆盖性配置,最常更改的几个常量属性是:
- 1.struts.i18n.encoding=UTF-8:请求消息的编码方式
- 2.struts.action.extension=action:指定被struts2处理的请求后缀类型。多个用逗号隔开。如:action,do,go
- 3.struts.configuration.xml.reload=false:当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开
- 4.struts.devMode=false:是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开
- 5.struts.serve.static.browserCache=true:设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭。
- 6.struts.objectFactory=spring:指定由spring负责action对象的创建
- 7.struts.enable.SlashesInActionNames=false:启用Action的name是否支持斜线(/)
- 8.struts.enable.DynamicMethodInvocation=false:启用动态方法调用
- 9.struts.ui.theme=simple:UI主题类型
- -->
- <constant name="struts.action.extension" value="action,do,htm" />
- <constant name="struts.configuration.xml.reload" value="true" />
- <constant name="struts.devMode" value="false" />
- <constant name="struts.serve.static.browserCache" value="false" />
-
-
- <!--
- package常用的属性:
- 1.name:包名,作为被其它包引用的标识。必须的。实际应用中,应该把一组业务功能相关的Action放在同一个包下。
- 2.extends:指定要扩展的包名。一般会继承自struts-default包。struts-default包是struts2内置的,它定义了struts2内部的众多拦截器和Result类型。
- 3.namespace:指定名称空间。Struts2中Action的请求URI映射由namespace和action名称两部分组成。如果不指定该属性,默认的命名空间为""(空字符串)。
- 4.abstract:声明包为抽象的。抽象包中不能配置action。
- -->
- <package name="front" namespace="/" extends="struts-default">
- <action name="index" class="com.yjd.nb.web.action.IndexAction">
- <result>/main.jsp</result>
- </action>
- </package>
- <package name="front.msgOpt" namespace="/msg" extends="struts-default">
- <action name="add" class="com.yjd.nb.web.action.MsgAction"
- method="add">
- <result name="list" type="redirect">/index.action</result>
- </action>
- </package>
- </struts>
<?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>
<!--
在struts.xml文件中可以使用<constant>标记来对default.properties中的常量属性做覆盖性配置,最常更改的几个常量属性是:
1.struts.i18n.encoding=UTF-8:请求消息的编码方式
2.struts.action.extension=action:指定被struts2处理的请求后缀类型。多个用逗号隔开。如:action,do,go
3.struts.configuration.xml.reload=false:当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开
4.struts.devMode=false:是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开
5.struts.serve.static.browserCache=true:设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭。
6.struts.objectFactory=spring:指定由spring负责action对象的创建
7.struts.enable.SlashesInActionNames=false:启用Action的name是否支持斜线(/)
8.struts.enable.DynamicMethodInvocation=false:启用动态方法调用
9.struts.ui.theme=simple:UI主题类型
-->
<constant name="struts.action.extension" value="action,do,htm" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.serve.static.browserCache" value="false" />
<!-- Struts2以package形式管理Action的配置 -->
<!--
package常用的属性:
1.name:包名,作为被其它包引用的标识。必须的。实际应用中,应该把一组业务功能相关的Action放在同一个包下。
2.extends:指定要扩展的包名。一般会继承自struts-default包。struts-default包是struts2内置的,它定义了struts2内部的众多拦截器和Result类型。
3.namespace:指定名称空间。Struts2中Action的请求URI映射由namespace和action名称两部分组成。如果不指定该属性,默认的命名空间为""(空字符串)。
4.abstract:声明包为抽象的。抽象包中不能配置action。
-->
<package name="front" namespace="/" extends="struts-default">
<action name="index" class="com.yjd.nb.web.action.IndexAction">
<result>/main.jsp</result>
</action>
</package>
<package name="front.msgOpt" namespace="/msg" extends="struts-default">
<action name="add" class="com.yjd.nb.web.action.MsgAction"
method="add">
<result name="list" type="redirect">/index.action</result>
</action>
</package>
</struts>
第五步:创建action类。
跳转到首页的action:
[java] view plain copy print ?
-
-
-
-
-
-
-
- package com.yjd.nb.web.action;
-
- import org.apache.struts2.ServletActionContext;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.yjd.domain.common.PageModel;
- import com.yjd.nb.domain.Message;
- import com.yjd.nb.service.MessageService;
-
-
-
-
-
-
- public class IndexAction extends ActionSupport {
- private static final long serialVersionUID = -3634083788212536690L;
- private MessageService messageService = new MessageService();
- private int pageNo = 1;
- private int pageSize = 10;
-
- public String execute() {
- PageModel<Message> pm = messageService.findByPage(pageNo, pageSize);
-
- ServletActionContext.getRequest().setAttribute("pm", pm);
-
- return SUCCESS;
- }
-
- public MessageService getMessageService() {
- return messageService;
- }
-
- public void setMessageService(MessageService messageService) {
- this.messageService = messageService;
- }
-
- public int getPageNo() {
- return pageNo;
- }
-
- public void setPageNo(int pageNo) {
- this.pageNo = pageNo;
- }
-
- public int getPageSize() {
- return pageSize;
- }
-
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- }
/**
* ClassName: IndexAction.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.web.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yjd.domain.common.PageModel;
import com.yjd.nb.domain.Message;
import com.yjd.nb.service.MessageService;
/**
* 首页的请求处理类
*
* @author yjd
*/
public class IndexAction extends ActionSupport {
private static final long serialVersionUID = -3634083788212536690L;
private MessageService messageService = new MessageService();
private int pageNo = 1;
private int pageSize = 10;
public String execute() {
PageModel<Message> pm = messageService.findByPage(pageNo, pageSize);
ServletActionContext.getRequest().setAttribute("pm", pm);
// ActionContext.getContext().put("pm", pm);
return SUCCESS;
}
public MessageService getMessageService() {
return messageService;
}
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
实体操作action:
[java] view plain copy print ?
-
-
-
-
-
-
-
- package com.yjd.nb.web.action;
-
- import java.util.Date;
-
- import org.apache.struts2.ServletActionContext;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.yjd.nb.domain.Message;
- import com.yjd.nb.service.MessageService;
-
-
-
-
-
-
- public class MsgAction extends ActionSupport {
- private static final long serialVersionUID = -5736885633645314440L;
- private MessageService messageService = new MessageService();
- private Message msg;
-
-
- public String add() {
- msg.setIp(ServletActionContext.getRequest().getRemoteAddr());
- msg.setPubTime(new Date());
-
- messageService.add(msg);
- return "list";
- }
-
- public Message getMsg() {
- return msg;
- }
-
- public void setMsg(Message msg) {
- this.msg = msg;
- }
-
- }
/**
* ClassName: MsgAction.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.web.action;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yjd.nb.domain.Message;
import com.yjd.nb.service.MessageService;
/**
* 前台针对Msg操作的处理类
*
* @author yjd
*/
public class MsgAction extends ActionSupport {
private static final long serialVersionUID = -5736885633645314440L;
private MessageService messageService = new MessageService();
private Message msg;
// 添加实体
public String add() {
msg.setIp(ServletActionContext.getRequest().getRemoteAddr());
msg.setPubTime(new Date());
messageService.add(msg);
return "list";
}
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
}
第六步:在web.xml中配置启动Struts2框架的过滤器。
[html] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第七步:编写JSP显示页面。
首页跳转index.jsp
[html] view plain copy print ?
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%--<jsp:forward page="index.action"/> --%>
- <meta http-equiv="refresh" content="0;url=index.action" />
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%--<jsp:forward page="index.action"/> --%>
<meta http-equiv="refresh" content="0;url=index.action" />
主页:main.jsp
[html] view plain copy print ?
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <%@taglib uri="http://hi.csdn.net/tjcyjd/tags" prefix="myTag" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html><head><title>在线留言系统</title>
- <meta http-equiv=content-type content="text/html; charset=UTF-8">
- <link href="images/style.css" type=text/css rel=stylesheet></link>
- <style type="text/css">
- body{font-size: 12px}
- table{font-size: 12px}
- a{font-size:12px}
- .current{font-size:12px;}
- </style>
-
- </head>
- <body>
- <form name=form1 action="msg/add.action" method=post>
- <table class=tab cellspacing=1 align=center border=0>
- <tbody>
- <tr>
- <td class=title background=images/titlebg.jpg colspan=2
- height=25><span>请 签 写 留 言</span> </td></tr>
- <tr>
- <td align=right width="20%">您的称呼: </td>
- <td width="80%"><input id="username" maxlength=50 name="msg.nickname"/> </td></tr>
- <tr>
- <td align=right width="20%">您的性别: </td>
- <td width="80%"><input id=sex type=radio value="true" name="msg.gender">
- 男 <input type=radio checked value="false" name="msg.gender"> 女
- </td></tr>
- <tr>
- <td align=right>选择头像: </td>
- <td><select id=image name="msg.header_img"> <option
- value=1.gif selected>1.gif</option> <option
- value=2.gif>2.gif</option> <option
- value=3.gif>3.gif</option> <option
- value=4.gif>4.gif</option> <option
- value=5.gif>5.gif</option> <option
- value=6.gif>6.gif</option> <option
- value=7.gif>7.gif</option> <option
- value=8.gif>8.gif</option> <option
- value=9.gif>9.gif</option> <option
- value=10.gif>10.gif</option> <option
- value=11.gif>11.gif</option> <option
- value=12.gif>12.gif</option> <option
- value=13.gif>13.gif</option> <option
- value=14.gif>14.gif</option> <option
- value=15.gif>15.gif</option> <option
- value=16.gif>16.gif</option> <option
- value=17.gif>17.gif</option> <option
- value=18.gif>18.gif</option> <option
- value=19.gif>19.gif</option> <option
- value=20.gif>20.gif</option></select> </td></tr>
- <tr>
- <td align=right>您的qq: </td>
- <td><input id=qq maxlength=50 name="msg.qq"> </td></tr>
- <tr>
- <td align=right>您的邮箱: </td>
- <td><input id=email maxlength=50 name="msg.email"> </td></tr>
- <tr>
- <td align=right>留言内容: </td>
- <td><textarea id=content name="msg.content" rows=5 cols=50></textarea>
- </td></tr>
- <tr>
- <td align="center" colspan=2><input type=submit value=提交>
- <input type=reset value=重置> </td></tr></tbody></table></form>
- <table class=tab cellspacing=1 align=center border=0>
- <tbody>
- <tr>
- <td class=title background=images/titlebg.jpg colspan=2
- height=25><span>留 言 列 表【<a href="login.jsp">管理登录</a>】 </span></td></tr>
-
- <c:forEach items="${pm.data}" var="msg">
- <tr>
- <td align="center" width="20%" rowspan=4>你好 : ${msg.nickname} ${msg.gender ? "帅哥" : "靓妹"} <br><br><img
- src="images/${msg.header_img}"> </td></tr>
- <tr>
- <td width="80%" height="100%">发表于: <fmt:formatDate value="${msg.pubTime}" pattern="yyyy-MM-dd HH:mm:ss"/> <img
- src="images/8_online.gif" border=0> <a href="mailto:${msg.email}"><img
- src="images/email.gif" border=0></a> <img src="images/ip.gif"
- border=0> 来自:${msg.ip}</td></tr>
- <tr>
- <td>${msg.content}</td></tr>
- <tr>
- <td><font color=#ff0000>管理员回复:</font> </td></tr>
- </c:forEach>
-
- </tbody></table>
-
- <myTag:pager pageSize="${pm.pageSize}" pageNo="${pm.pageNo}" url="index.action" recordCount="${pm.recordCount}"/>
-
- </body></html>
下面就简单学习下Struts2+Hibernate的整合。
其实就七个步骤:
第一步:添加所需的jar包。
第二步:创建实体类以及对应的映射文件。
第三步:配置hibernate.cfg.xml配置文件。
第四步:配置struts.xml配置文件。
第五步:创建相应的action。
第六步:在web.xml中配置启动Struts2框架的过滤器。
第七步:编写JSP显示页面。
---------------------------------------------------------------------------------------------------------------------------
具体细节及代码如下(代码仅供参考):
第一步:jar包的添加这里不多说。
第二步:
实体类Message.java
/**
* ClassName: Message.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.domain;
import java.io.Serializable;
import java.util.Date;
/**
* 留言实体类
*
* @author yjd
*/
public class Message implements Serializable {
private static final long serialVersionUID = 6617253915349620625L;
private Integer id; // OID
private String nickname; // 昵称
private boolean gender; // 性别
private String header_img; // 头像
private String qq; // qq
private String email; // 邮箱号
private String content; // 留言内容
private String ip; // 发布者的IP地址
private Date pubTime; // 留言时间
private String reply;// 管理员回复内容
private Date replyTime; // 回复时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public String getHeader_img() {
return header_img;
}
public void setHeader_img(String headerImg) {
header_img = headerImg;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Date getPubTime() {
return pubTime;
}
public void setPubTime(Date pubTime) {
this.pubTime = pubTime;
}
public String getReply() {
return reply;
}
public void setReply(String reply) {
this.reply = reply;
}
public Date getReplyTime() {
return replyTime;
}
public void setReplyTime(Date replyTime) {
this.replyTime = replyTime;
}
@Override
public String toString() {
return "Message [content=" + content + ", email=" + email + ", gender="
+ gender + ", header_img=" + header_img + ", id=" + id
+ ", ip=" + ip + ", nickname=" + nickname + ", pubTime="
+ pubTime + ", qq=" + qq + "]";
}
}
实体类映射文件Message.hbm.xml.
[html] view plain copy print ?
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
-
- <hibernate-mapping>
- <class name="com.yjd.nb.domain.Message" table="msg">
- <id name="id" column="id">
- <generator class="native" />
- </id>
-
- <property name="nickname" />
- <property name="gender" />
- <property name="header_img" />
- <property name="qq" />
- <property name="email" />
- <property name="ip" />
- <property name="content" />
- <property name="pubTime" column="pub_time" />
- <property name="reply" />
- <property name="replyTime" column="reply_time" />
- </class>
- </hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yjd.nb.domain.Message" table="msg">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="nickname" />
<property name="gender" />
<property name="header_img" />
<property name="qq" />
<property name="email" />
<property name="ip" />
<property name="content" />
<property name="pubTime" column="pub_time" />
<property name="reply" />
<property name="replyTime" column="reply_time" />
</class>
</hibernate-mapping>
第三步:Hibernate配置文件hibernate.cfg.xml.
[html] view plain copy print ?
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-
- <hibernate-configuration>
- <session-factory>
-
- <property name="hibernate.connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="hibernate.connection.url">
- jdbc:mysql:///s2h-model
- </property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
-
-
- <property name="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
-
-
- <property name="connection.provider_class">
- org.hibernate.connection.C3P0ConnectionProvider
- </property>
- <property name="hibernate.c3p0.min_size">5</property>
- <property name="hibernate.c3p0.max_size">30</property>
- <property name="hibernate.c3p0.timeout">1800</property>
- <property name="hibernate.c3p0.max_statements">50</property>
-
-
- <property name="hbm2ddl.auto">update</property>
- <property name="show_sql">true</property>
-
-
-
- <mapping resource="com/yjd/nb/domain/Message.hbm.xml" />
-
- </session-factory>
- </hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库的连接参数配置 -->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql:///s2h-model
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- c3p0连接池的配置 -->
<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<!-- 其它配置 -->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<!-- <property name="format_sql">true</property> -->
<!-- 注册映射文件 -->
<mapping resource="com/yjd/nb/domain/Message.hbm.xml" />
</session-factory>
</hibernate-configuration>
第四步:Struts2配置文件struts.xml.
[html] view plain copy print ?
- <?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>
- <!--
- 在struts.xml文件中可以使用<constant>标记来对default.properties中的常量属性做覆盖性配置,最常更改的几个常量属性是:
- 1.struts.i18n.encoding=UTF-8:请求消息的编码方式
- 2.struts.action.extension=action:指定被struts2处理的请求后缀类型。多个用逗号隔开。如:action,do,go
- 3.struts.configuration.xml.reload=false:当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开
- 4.struts.devMode=false:是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开
- 5.struts.serve.static.browserCache=true:设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭。
- 6.struts.objectFactory=spring:指定由spring负责action对象的创建
- 7.struts.enable.SlashesInActionNames=false:启用Action的name是否支持斜线(/)
- 8.struts.enable.DynamicMethodInvocation=false:启用动态方法调用
- 9.struts.ui.theme=simple:UI主题类型
- -->
- <constant name="struts.action.extension" value="action,do,htm" />
- <constant name="struts.configuration.xml.reload" value="true" />
- <constant name="struts.devMode" value="false" />
- <constant name="struts.serve.static.browserCache" value="false" />
-
-
- <!--
- package常用的属性:
- 1.name:包名,作为被其它包引用的标识。必须的。实际应用中,应该把一组业务功能相关的Action放在同一个包下。
- 2.extends:指定要扩展的包名。一般会继承自struts-default包。struts-default包是struts2内置的,它定义了struts2内部的众多拦截器和Result类型。
- 3.namespace:指定名称空间。Struts2中Action的请求URI映射由namespace和action名称两部分组成。如果不指定该属性,默认的命名空间为""(空字符串)。
- 4.abstract:声明包为抽象的。抽象包中不能配置action。
- -->
- <package name="front" namespace="/" extends="struts-default">
- <action name="index" class="com.yjd.nb.web.action.IndexAction">
- <result>/main.jsp</result>
- </action>
- </package>
- <package name="front.msgOpt" namespace="/msg" extends="struts-default">
- <action name="add" class="com.yjd.nb.web.action.MsgAction"
- method="add">
- <result name="list" type="redirect">/index.action</result>
- </action>
- </package>
- </struts>
<?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>
<!--
在struts.xml文件中可以使用<constant>标记来对default.properties中的常量属性做覆盖性配置,最常更改的几个常量属性是:
1.struts.i18n.encoding=UTF-8:请求消息的编码方式
2.struts.action.extension=action:指定被struts2处理的请求后缀类型。多个用逗号隔开。如:action,do,go
3.struts.configuration.xml.reload=false:当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开
4.struts.devMode=false:是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开
5.struts.serve.static.browserCache=true:设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭。
6.struts.objectFactory=spring:指定由spring负责action对象的创建
7.struts.enable.SlashesInActionNames=false:启用Action的name是否支持斜线(/)
8.struts.enable.DynamicMethodInvocation=false:启用动态方法调用
9.struts.ui.theme=simple:UI主题类型
-->
<constant name="struts.action.extension" value="action,do,htm" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.serve.static.browserCache" value="false" />
<!-- Struts2以package形式管理Action的配置 -->
<!--
package常用的属性:
1.name:包名,作为被其它包引用的标识。必须的。实际应用中,应该把一组业务功能相关的Action放在同一个包下。
2.extends:指定要扩展的包名。一般会继承自struts-default包。struts-default包是struts2内置的,它定义了struts2内部的众多拦截器和Result类型。
3.namespace:指定名称空间。Struts2中Action的请求URI映射由namespace和action名称两部分组成。如果不指定该属性,默认的命名空间为""(空字符串)。
4.abstract:声明包为抽象的。抽象包中不能配置action。
-->
<package name="front" namespace="/" extends="struts-default">
<action name="index" class="com.yjd.nb.web.action.IndexAction">
<result>/main.jsp</result>
</action>
</package>
<package name="front.msgOpt" namespace="/msg" extends="struts-default">
<action name="add" class="com.yjd.nb.web.action.MsgAction"
method="add">
<result name="list" type="redirect">/index.action</result>
</action>
</package>
</struts>
第五步:创建action类。
跳转到首页的action:
[java] view plain copy print ?
-
-
-
-
-
-
-
- package com.yjd.nb.web.action;
-
- import org.apache.struts2.ServletActionContext;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.yjd.domain.common.PageModel;
- import com.yjd.nb.domain.Message;
- import com.yjd.nb.service.MessageService;
-
-
-
-
-
-
- public class IndexAction extends ActionSupport {
- private static final long serialVersionUID = -3634083788212536690L;
- private MessageService messageService = new MessageService();
- private int pageNo = 1;
- private int pageSize = 10;
-
- public String execute() {
- PageModel<Message> pm = messageService.findByPage(pageNo, pageSize);
-
- ServletActionContext.getRequest().setAttribute("pm", pm);
-
- return SUCCESS;
- }
-
- public MessageService getMessageService() {
- return messageService;
- }
-
- public void setMessageService(MessageService messageService) {
- this.messageService = messageService;
- }
-
- public int getPageNo() {
- return pageNo;
- }
-
- public void setPageNo(int pageNo) {
- this.pageNo = pageNo;
- }
-
- public int getPageSize() {
- return pageSize;
- }
-
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- }
/**
* ClassName: IndexAction.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.web.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yjd.domain.common.PageModel;
import com.yjd.nb.domain.Message;
import com.yjd.nb.service.MessageService;
/**
* 首页的请求处理类
*
* @author yjd
*/
public class IndexAction extends ActionSupport {
private static final long serialVersionUID = -3634083788212536690L;
private MessageService messageService = new MessageService();
private int pageNo = 1;
private int pageSize = 10;
public String execute() {
PageModel<Message> pm = messageService.findByPage(pageNo, pageSize);
ServletActionContext.getRequest().setAttribute("pm", pm);
// ActionContext.getContext().put("pm", pm);
return SUCCESS;
}
public MessageService getMessageService() {
return messageService;
}
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
实体操作action:
[java] view plain copy print ?
-
-
-
-
-
-
-
- package com.yjd.nb.web.action;
-
- import java.util.Date;
-
- import org.apache.struts2.ServletActionContext;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.yjd.nb.domain.Message;
- import com.yjd.nb.service.MessageService;
-
-
-
-
-
-
- public class MsgAction extends ActionSupport {
- private static final long serialVersionUID = -5736885633645314440L;
- private MessageService messageService = new MessageService();
- private Message msg;
-
-
- public String add() {
- msg.setIp(ServletActionContext.getRequest().getRemoteAddr());
- msg.setPubTime(new Date());
-
- messageService.add(msg);
- return "list";
- }
-
- public Message getMsg() {
- return msg;
- }
-
- public void setMsg(Message msg) {
- this.msg = msg;
- }
-
- }
/**
* ClassName: MsgAction.java
* Created on 2011
* Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: [email protected]
*/
package com.yjd.nb.web.action;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yjd.nb.domain.Message;
import com.yjd.nb.service.MessageService;
/**
* 前台针对Msg操作的处理类
*
* @author yjd
*/
public class MsgAction extends ActionSupport {
private static final long serialVersionUID = -5736885633645314440L;
private MessageService messageService = new MessageService();
private Message msg;
// 添加实体
public String add() {
msg.setIp(ServletActionContext.getRequest().getRemoteAddr());
msg.setPubTime(new Date());
messageService.add(msg);
return "list";
}
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
}
第六步:在web.xml中配置启动Struts2框架的过滤器。
[html] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第七步:编写JSP显示页面。
首页跳转index.jsp
[html] view plain copy print ?
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%--<jsp:forward page="index.action"/> --%>
- <meta http-equiv="refresh" content="0;url=index.action" />
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%--<jsp:forward page="index.action"/> --%>
<meta http-equiv="refresh" content="0;url=index.action" />
主页:main.jsp
[html] view plain copy print ?
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
- <%@taglib uri="http://hi.csdn.net/tjcyjd/tags" prefix="myTag" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html><head><title>在线留言系统</title>
- <meta http-equiv=content-type content="text/html; charset=UTF-8">
- <link href="images/style.css" type=text/css rel=stylesheet></link>
- <style type="text/css">
- body{font-size: 12px}
- table{font-size: 12px}
- a{font-size:12px}
- .current{font-size:12px;}
- </style>
-
- </head>
- <body>
- <form name=form1 action="msg/add.action" method=post>
- <table class=tab cellspacing=1 align=center border=0>
- <tbody>
- <tr>
- <td class=title background=images/titlebg.jpg colspan=2
- height=25><span>请 签 写 留 言</span> </td></tr>
- <tr>
- <td align=right width="20%">您的称呼: </td>
- <td width="80%"><input id="username" maxlength=50 name="msg.nickname"/> </td></tr>
- <tr>
- <td align=right width="20%">您的性别: </td>
- <td width="80%"><input id=sex type=radio value="true" name="msg.gender">
- 男 <input type=radio checked value="false" name="msg.gender"> 女
- </td></tr>
- <tr>
- <td align=right>选择头像: </td>
- <td><select id=image name="msg.header_img"> <option
- value=1.gif selected>1.gif</option> <option
- value=2.gif>2.gif</option> <option
- value=3.gif>3.gif</option> <option
- value=4.gif>4.gif</option> <option
- value=5.gif>5.gif</option> <option
- value=6.gif>6.gif</option> <option
- value=7.gif>7.gif</option> <option
- value=8.gif>8.gif</option> <option
- value=9.gif>9.gif</option> <option
- value=10.gif>10.gif</option> <option
- value=11.gif>11.gif</option> <option
- value=12.gif>12.gif</option> <option
- value=13.gif>13.gif</option> <option
- value=14.gif>14.gif</option> <option
- value=15.gif>15.gif</option> <option
- value=16.gif>16.gif</option> <option
- value=17.gif>17.gif</option> <option
- value=18.gif>18.gif</option> <option
- value=19.gif>19.gif</option> <option
- value=20.gif>20.gif</option></select> </td></tr>
- <tr>
- <td align=right>您的qq: </td>
- <td><input id=qq maxlength=50 name="msg.qq"> </td></tr>
- <tr>
- <td align=right>您的邮箱: </td>
- <td><input id=email maxlength=50 name="msg.email"> </td></tr>
- <tr>
- <td align=right>留言内容: </td>
- <td><textarea id=content name="msg.content" rows=5 cols=50></textarea>
- </td></tr>
- <tr>
- <td align="center" colspan=2><input type=submit value=提交>
- <input type=reset value=重置> </td></tr></tbody></table></form>
- <table class=tab cellspacing=1 align=center border=0>
- <tbody>
- <tr>
- <td class=title background=images/titlebg.jpg colspan=2
- height=25><span>留 言 列 表【<a href="login.jsp">管理登录</a>】 </span></td></tr>
-
- <c:forEach items="${pm.data}" var="msg">
- <tr>
- <td align="center" width="20%" rowspan=4>你好 : ${msg.nickname} ${msg.gender ? "帅哥" : "靓妹"} <br><br><img
- src="images/${msg.header_img}"> </td></tr>
- <tr>
- <td width="80%" height="100%">发表于: <fmt:formatDate value="${msg.pubTime}" pattern="yyyy-MM-dd HH:mm:ss"/> <img
- src="images/8_online.gif" border=0> <a href="mailto:${msg.email}"><img
- src="images/email.gif" border=0></a> <img src="images/ip.gif"
- border=0> 来自:${msg.ip}</td></tr>
- <tr>
- <td>${msg.content}</td></tr>
- <tr>
- <td><font color=#ff0000>管理员回复:</font> </td></tr>
- </c:forEach>
-
- </tbody></table>
-
- <myTag:pager pageSize="${pm.pageSize}" pageNo="${pm.pageNo}" url="index.action" recordCount="${pm.recordCount}"/>
-
- </body></html>