步骤:
1、使用myeclipse 配置Struts 2框架。
2、使用myeclipse配置hibernate框架。
3、使用myeclipse反向生成实体对象的方法,结合表之间的关系,创建数据表的实体对象,以及表的映射文件。
3、创建相应的Action。
4、编写JSP显示页面。
步骤1,2,3在博客中有相应的说明,自行查看。
具体细节及代码如下:(代码仅供参考)
一、搭建Struts 2+hibernate环境
1、Struts 2配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.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主题类型
-->
<!-- 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。
-->
<constant name="struts.i18n.encoding" value="gb2312" />
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="loginCheck" class="interceptor.LoginCheck"></interceptor>
<interceptor name="managerLoginCheck" class="interceptor.ManagerLoginCheck"></interceptor>
</interceptors>
<global-results>
<result name="error" type="redirect">/error.html</result>
</global-results>
<action name="enrollAction" class="action.EnrollAction">
<result name="input">/enroll.jsp</result>
<result name="success">/login.jsp</result>
</action>
<action name="loginAction" class="action.LoginAction">
<result name="input">/login.jsp</result>
<result name="success" type="redirect">/action/allPostAction.action</result>
</action>
<action name="managerLoginAction" class="action.ManagerLoginAction">
<result name="input">/managerLogin.jsp</result>
<result name="success" type="redirect">/action/allPostAction.action</result>
</action>
<action name="userInformationAction" class="action.UserInformationAction">
<result name="success">/userInformation.jsp</result>
</action>
<action name="personalInformationAction" class="action.PersonalInformationAction">
<interceptor-ref name="loginCheck"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="fail" type="redirect">/action/allPostAction.action</result>
<result name="success" type="redirect">/myInformation.jsp</result>
</action>
<action name="personalManageAction" class="action.PersonalManageAction">
<interceptor-ref name="loginCheck"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="fail" type="redirect">/action/allPostAction.action</result>
<result name="input">/myInformation.jsp</result>
<result name="success" type="redirect">/action/personalInformationAction.action</result>
</action>
<action name="updatePasswordAction" class="action.UpdatePasswordAction">
<interceptor-ref name="loginCheck"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="fail" type="redirect">/action/allPostAction.action</result>
<result name="input">/updatePassword.jsp</result>
<result name="success" type="redirect">/action/personalInformationAction.action</result>
</action>
<action name="personalPostAction" class="action.PersonalPostAction">
<interceptor-ref name="loginCheck"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="fail" type="redirect">/action/allPostAction.action</result>
<result name="success">/myPost.jsp</result>
</action>
<action name="allPostAction" class="action.AllPostAction">
<result name="success">/allPost.jsp</result>
</action>
<action name="addPostAction" class="action.AddPostAction">
<interceptor-ref name="loginCheck"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="input">/newPost.jsp</result>
<result name="fail">/newPost.jsp</result>
<result name="success" type="redirect">/newPost.jsp</result>
</action>
<action name="deleteMyPostAction" class="action.DeleteMyPostAction">
<interceptor-ref name="loginCheck"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success" type="redirect">/action/personalPostAction.action?pageNumber=${pageNumber}</result>
</action>
<action name="deletePostAction" class="action.DeletePostAction">
<interceptor-ref name="managerLoginCheck"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success" type="redirect">/action/allPostAction.action?pageNumber=${pageNumber}</result>
</action>
<action name="postMatterAction" class="action.PostMatterAction">
<result name="success">/postMatter.jsp</result>
</action>
<action name="userExitAction" class="action.ExitAction" method="userExit">
<result name="success" type="redirect">/action/allPostAction.action</result>
</action>
<action name="managerExitAction" class="action.ExitAction" method="managerExit">
<result name="success" type="redirect">/action/allPostAction.action</result>
</action>
</package>
<!-- Add packages here -->
</struts>
2、在web.xml中配置Struts2框架的核心控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>
3、hibernate配置hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
<mapping resource="entity/Manager.hbm.xml" />
<mapping resource="entity/User.hbm.xml" />
<mapping resource="entity/Post.hbm.xml" />
</session-factory>
</hibernate-configuration>
二、建立业务实体对象
4、实体类Manager.java
package entity;
/**
* Manager entity. @author MyEclipse Persistence Tools
*/
@SuppressWarnings("serial")
public class Manager implements java.io.Serializable {
// Fields
private Integer managerId;
private String managerName;
private String managerPassword;
// Constructors
/** default constructor */
public Manager() {
}
/** full constructor */
public Manager(String managerName, String managerPassword) {
this.managerName = managerName;
this.managerPassword = managerPassword;
}
// Property accessors
public Integer getManagerId() {
return this.managerId;
}
public void setManagerId(Integer managerId) {
this.managerId = managerId;
}
public String getManagerName() {
return this.managerName;
}
public void setManagerName(String managerName) {
this.managerName = managerName;
}
public String getManagerPassword() {
return this.managerPassword;
}
public void setManagerPassword(String managerPassword) {
this.managerPassword = managerPassword;
}
}
5、实体类的映射文件Manager.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="entity.Manager" table="manager" catalog="forum">
<id name="managerId" type="java.lang.Integer">
<column name="managerId" />
<generator class="native" />
</id>
<property name="managerName" type="java.lang.String">
<column name="managerName" length="16" not-null="true" />
</property>
<property name="managerPassword" type="java.lang.String">
<column name="managerPassword" length="12" not-null="true" />
</property>
</class>
</hibernate-mapping>
三、创建业务逻辑控制器Action
6、创建Action
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dao.ForumDao;
import entity.User;
@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute(){
return SUCCESS;
}
public void validate(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ForumDao forumDao = new ForumDao();
if(user != null){
User user2 = forumDao.judgeUser(user); //判断是否已经有用户登录
if(user2 != null){ //如果已经有用户登录
if(session.getAttribute("loginUser") != null){ //
session.removeAttribute("loginUser"); //清除已有登录的用户
}
session.setAttribute("loginUser", user2);
}else{
addFieldError("user.userName", "用户名或密码有误!");
}
}
}
}
四、编写显示页面
7、编写JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户登录</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<center>
<%@ include file="top.jsp" %>
<div id="wholePage">
<div id="currentPoint">
当前位置: <a class="green" href="action/allPostAction.action">首页</a> --> 用户登录
</div>
<h1>用户登录</h1>
<hr width="400"/>
<s:form action="loginAction" method="post">
<s:textfield label="用户名" name="user.userName" maxlength="16"></s:textfield>
<s:password label="密码" name="user.userPassword" maxlength="12"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
</div>
<%@ include file="bottom.html" %>
</center>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<jsp:forward page="firstPage.jsp"></jsp:forward>
</body>
</html>
项目结构如图: