ssh框架的小实例(用户登录)
Posted 流云10
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh框架的小实例(用户登录)相关的知识,希望对你有一定的参考价值。
刚学SSH框架写一个小实例,以便以后查看:
本案例简单的实现一个用户登录:
数据库方面就不写了,自己领悟吧!哈哈(根据user.hbm.xml文件就知道了)
我们一般可以创建下面几个包,什么意思呢,自己悟
com.website.action.user
com.website.dao
com.website.dao.impl
com.website.entity
com.website.service
com.website.service.impl
下面开始码代码了:
com.website.entity包中User.java与user.hbm.xml
User.java
package com.website.entity;
public class User {
private int userId;
private String userName;
private String userPassword;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
user.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">
<hibernate-mapping>
<class name="com.website.entity.User" table="user">
<id name="userId" column="user_id" type="int">
<generator class="identity"></generator>
</id>
<property name="userName" column="user_name" type="string"/>
<property name="userPassword" column="user_password" type="string"/>
</class>
</hibernate-mapping>
com.website.dao包中的UserDAO.java接口
UserDAO.java
package com.website.dao;
import com.website.entity.User;
/**
* 所需要的user用户的DAO接口
* 包含findUserByID,loginUser
* @author 战旗
*
*/
public interface UserDAO {
public User findUserById(Integer id);
public User loginUser(User user);
}
com.website.dao.impl包中的UserDAOImpl.java
package com.website.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.website.dao.UserDAO;
import com.website.entity.User;
/**
* userDAO 接口的实现类
* @author 战旗
*
*/
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{
/**
* @param id
* 根据ID查找User
* @return User
*/
@Override
public User findUserById(Integer id) {
User user = (User)this.getHibernateTemplate().get(User.class, id);
return user;
}
/**
* @param user
* 根据user的用户名 和 密码查找user用户,
* 如果查到返回user,否则返回null
* @return user
*/
@SuppressWarnings("unchecked")
@Override
public User loginUser(User user) {
//sql语句
String hql = "from User user where user.userName=‘" + user.getUserName()
+ "‘ and user.userPassword=‘" + user.getUserPassword() + "‘" ;
List<User> users = (List<User>)this.getHibernateTemplate().find(hql);
if(users.size() > 0){
return users.get(0);
}
return null;
}
}
com.website.service包中的UserService.java接口
package com.website.service;
import com.website.entity.User;
/**
* User的Service接口
* @author 战旗
*
*/
public interface UserService {
public User findById(Integer id);
public User loginUser(User user);
}
com.website.service.impl包中的UserServiceImpl.java
package com.website.service.impl;
import com.website.dao.UserDAO;
import com.website.entity.User;
import com.website.service.UserService;
/**
*
* @author 战旗
*
*/
public class UserServiceImpl implements UserService{
private UserDAO userDAO;
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public User findById(Integer id) {
return this.userDAO.findUserById(id);
}
@Override
public User loginUser(User user) {
return this.userDAO.loginUser(user);
}
}
最后com.website.action.user包的UserAction.java
package com.website.action.user;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.website.entity.User;
import com.website.service.UserService;
@SuppressWarnings("serial")
public class UserAction extends ActionSupport{
private User user;
private UserService userService;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String login() throws Exception{
User userLogin = userService.loginUser(user);
if(userLogin != null){
HttpServletRequest req = ServletActionContext.getRequest();
HttpSession session = req.getSession();
session.setAttribute("user", userLogin);
return "success";
}else{
ActionContext.getContext().put("result", "2");
}
return "input";
}
}
来一下前端吧,详细点完整点:
<s:form action="loginUserAction" method="post">
<h2>登录</h2
<div style="margin:5px 0px;">
<input type="text" name="user.userName" placeholder="请输入账号..."/>
</div>
<div style="margin:5px 0px;">
<input type="password" name="user.userPassword" placeholder="请输入密码 ?..."/>
</div>
<!-- 验证码的提示信息 -->
<div id="vdmsg" style="display:inline; color: red;"></div>
<div style="margin: 5px 0px;">
<button type="submit" >登<span style="width:20px;"></span>录</button>
</div>
</s:form>
再加上错误提示信息吧!
<script type="text/javascript">
var result = "${result}";
if(result == "2"){
document.getElementById("vdmsg").innerhtml = "用户名 或 密码错误!";
}
</script>
下面是整个程序的重点了,struts.xml和applicationContext.xml文件的配置:
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>
<package name="website" extends="struts-default">
<action name="*UserAction" class="userAction" method="{1}">
<result name="success" type="redirect">/main.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/website</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/website/entity/user.hbm.xml</value>
<value>com/website/entity/admin.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- userDao -->
<bean id="userDao" class="com.website.dao.impl.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="userServiceTarget" class="com.website.service.impl.UserServiceImpl">
<property name="userDAO" ref="userDao"/>
</bean>
<bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="userServiceTarget"></property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userAction" class="com.website.action.user.UserAction"
scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
</beans>
到此就完全配置完了,web.xml的配置自己去吧配置个struts的过滤器就ok了
以上是关于ssh框架的小实例(用户登录)的主要内容,如果未能解决你的问题,请参考以下文章
管理系统-------------SSH框架书写登录和显示用户
使用root用户通过SSH登录Linux实例时报“Permission denied, please try again”的错误