按照上一篇所写的内容配置好SSH开发环境
http://blog.csdn.net/zk673820543/article/details/51218358
源码下载
http://download.csdn.net/detail/zk673820543/9499360
1、创建数据库
新建数据库,命名为sshtest
CREATE DATABASE sshtest DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
新建表user_info,sql如下:
CREATE TABLE `t_user` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`password` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
出入一条数据
insert into t_user values(1,‘LJaer‘,‘123‘);
2、建立数据库连接
在MyEclipse中,将视图切换到MyEclipse Database Explorer,新建一个数据库连接
由于我这里数据库没设置密码,所以password那里就空下
由于我自己的mysql数据库没设置密码,所以没填
直接Finish。
在左侧双击刚才新增的数据库连接,输入密码,就可以看到ssh下面的t_user表了
hibernate.cfg.xml根据实际情况进行修改
在src下新建一个package,model.system
回到数据库视图,右键点击t_user表,选择Hibernate Reverse Engineering
直接点Finish。
现在可以看到已经添加好的文件
5、修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>test</display-name> <!-- 首页设置 --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- spring相关配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 以下3项参数与log4j的配置相关 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <span style="white-space:pre"> </span><!-- end --> <span style="white-space:pre"> </span> <!-- struts2相关配置 --> <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> </web-app>
6.修改applicationContext.xml
内容如下
<?xml version="1.0" encoding="UTF-8"?> <beans <span style="white-space:pre"> </span>xmlns="http://www.springframework.org/schema/beans" <span style="white-space:pre"> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <span style="white-space:pre"> </span>xmlns:p="http://www.springframework.org/schema/p" <span style="white-space:pre"> </span>xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!--指明hibernate配置文件的位置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="mappingResources"> <span style="white-space:pre"> </span><list> <span style="white-space:pre"> </span><value>model/system/TUser.hbm.xml</value></list> </property></bean> <!--定义DAO --> <bean id="userDao" class="dao.system.UserDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置LoginAction.java类文件 --> <bean id="LoginAction" class="action.login.LoginAction" scope="prototype"> <property name="userService" ref="userService" /> </bean> <!-- 配置service层 --> <bean id="userService" class="service.system.UserService"> <property name="userDao"> <ref bean="userDao"/> </property> </bean> </beans>
7、.添加dao,service和action
结构如下:
UserDao.java内容如下:
package dao.system; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import model.system.TUser; public class UserDao extends HibernateDaoSupport{ public void add(TUser m) { getHibernateTemplate().save(m); } @SuppressWarnings("unchecked") public List<TUser> showAllUsers() { return getHibernateTemplate().find("from user"); } public void update(TUser m) { getHibernateTemplate().update(m); } public void delete(String id) { getHibernateTemplate().delete(getHibernateTemplate().get(TUser.class, id)); } public TUser getUserById(String id) { TUser m=(TUser)getHibernateTemplate().get(TUser.class, id); return m; } //普通用户登录验证 @SuppressWarnings("unchecked") public boolean checkUser(String userName,String userPassword){ boolean flag = false; String hql = "from TUser as user where user.username = '"+userName+"' and user.password = '"+userPassword+"'"; List<TUser> userList = this.getHibernateTemplate().find(hql); if(userList.size()>0){ flag = true; } return flag; } }
UserService.java内容如下:
package service.system; import dao.system.UserDao; public class UserService{ private UserDao userDao; public boolean checkUser(String userName,String userPassword) { return userDao.checkUser(userName, userPassword); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
LoginAction.java内容如下:
package action.login; import com.opensymphony.xwork2.ActionSupport; import service.system.UserService; public class LoginAction extends ActionSupport{ private static final long serialVersionUID = 3989633817331294940L; private String username; private String password; private UserService userService = new UserService(); public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute(){ boolean flag = false; if (username == null || username.trim().equals("")) { this.addFieldError("username", "用户名不能为空"); return "fail"; } else if (password == null || password.trim().equals("")) { this.addFieldError("password", "密码不能为空"); return "fail"; } else { flag = userService.checkUser(username, password); if (flag) { return "success"; } else { return "fail"; } } } }
9.修改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> <!--下述语句是将struts2交给spring管理 --> <constant name="struts.objectFactory" value="spring" /> <!--将action内容放在package元素下,package元素的name值与extends值可以使用默认值,如下所示 --> <package name="ssh" extends="struts-default"> <!--例如与用户登录相关的LoginAction,配置如下,其中,class值与applicationContext.xml文件中该action的id值相对应,name值作为标识--> <action name="loginAction" class="LoginAction"> <!--result元素设置页面跳转值,其中name值为目标页面的字符串标识,如下,登录失败时回到fail.jsp页面,成功时,转向success.jsp页面 --> <result name="fail">/jsp/login/fail.jsp</result> <result name="success">/jsp/login/success.jsp</result> </action> </package> </struts>
10.添加页面
在WebRoot文件夹下面新建一个jsp文件夹,存放页面。
结构如下:
fail.jsp内容如下:
<%@ page language="java" pageEncoding="utf-8"%> <% 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>Fail</title> </head> <body> 登陆失败。<br> </body> </html>
success.jsp内容如下:
<%@ page language="java" pageEncoding="utf-8"%> <% 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>Success</title> </head> <body> 登陆成功。<br> </body> </html>
修改index.jsp,内容如下:
<%@ page language="java" pageEncoding="utf-8"%> <!-- 导入struts2标签库 --> <%@taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <title>用户登录</title> </head> <body> <center> <div id="login"> <s:form action="loginAction" method="post"> <s:textfield label="用户名" name="username"></s:textfield> <s:password label="密码" name="password"></s:password> <s:submit value="登录"></s:submit> </s:form> </div> </center> </body> </html>
11.新增log4j.properties文件
放在src下
内容如下:
log4j.rootLogger=DEUBG, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern=‘.‘yyyy-MM-dd‘.log‘
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern=‘.‘yyyy-MM-dd‘.log‘
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=${catalina.base}/logs/manager.
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern=‘.‘yyyy-MM-dd‘.log‘
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern=‘.‘yyyy-MM-dd‘.log‘
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=INFO, MANAGER
INFO, HOST-MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=INFO, HOST-MANAGER
12、部署运行