`

SSH 组合备忘.(spring2.5 struts2.0.11 hibernate3.2.3 )

阅读更多

1.所有的jar 包

 

 

j2ee jar 包中的其它没用的删掉.

2.struts.properties

struts.custom.i18n.resources=globalMessages
struts.objectFactory=spring
struts.locale=en_utf-8

3.struts.xml

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />

<constant name="struts.i18n.encoding" value="UTF-8" />

    <package name="SSH2" extends="struts-default">
       <action name="login" class="loginAction">
            <result>/addNews.jsp</result>
            <result name="input">/index.jsp</result>
    <result name="success">/ok.jsp</result>
       </action>
    </package>
</struts>

4.web.xml

<?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">
<display-name>common bussiness manage system</display-name>
<filter>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
   </filter-class>
</filter>

<filter>
   <filter-name>struts-cleanup</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.ActionContextCleanUp
   </filter-class>
</filter>

    <filter>

       <filter-name>encodingFilter</filter-name>

       <filter-class>

           org.springframework.web.filter.CharacterEncodingFilter

       </filter-class>

       <init-param>

           <param-name>encodingFilter</param-name>

           <param-value>UTF-8</param-value>

       </init-param>

    </filter>


<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
   <filter-name>struts-cleanup</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
   <error-code>404</error-code>
   <location>/404.jsp</location>
</error-page>

</web-app>

5.aplicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="myDataSource1"
   class="org.apache.commons.dbcp.BasicDataSource"
   destroy-method="close">
   <property name="driverClassName" value="com.mysql.jdbc.Driver" />
   <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
   <property name="username" value="root" />
   <property name="password" value="root" />
</bean>

<!-- 管理Hibernate -->

<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="myDataSource1"></property>
   <property name="mappingResources">
    <list>
     <value>ssh2/news/vo/User.hbm.xml</value>
     <value>ssh2/news/vo/News.hbm.xml</value>
    </list>
   </property>
   <property name="hibernateProperties">
    <value>
     hibernate.dialect=org.hibernate.dialect.MySQLDialect
    </value>
   </property>

</bean>

    <bean id="userdao" class="ssh2.news.dao.impl.UserDaoImpl">

       <property name="sessionFactory">
           <ref bean="sessionFactory"/>
       </property>

    </bean>

    <bean id="userservice" class="ssh2.news.service.impl.UserServiceImpl">

       <property name="userDao">

           <ref bean="userdao"/>

       </property>

    </bean>

    <bean id="loginAction" class="ssh2.news.action.LoginAction">
       <property name="userService">
           <ref bean="userservice"/>
       </property>
    </bean>

</beans>

6.index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<s:head />
<title>           

</title>
</head>
<body bgcolor="#3399CC">
<center>

    <div style="color: red"><s:fielderror /><s:actionmessage /></div>

   <s:form action="login" method="post">

    <s:textfield name="user.uid" label="UID" tooltip="ENTER YOUR UID" />

    <s:password name="user.password" label="PASSWORD"

       tooltip="ENTER YOUR PASSWORD" />

    <s:submit></s:submit>

</s:form>

<s:a href="regist.jsp">REGIST NEW COUNT</s:a></center>

</body>
</html>

7.java文件结构

8.action

package ssh2.news.action;

import java.util.Map;

import ssh2.news.service.UserService;

import ssh2.news.vo.User;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**

* 登录Action

* @author bulktree

*

*/

public class LoginAction 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;

    }

    @Override

    public String execute() throws Exception {

       if (isInvalid(user.getUid())) {

           this.addFieldError("uid", "登录ID不能为空");

           return INPUT;

       }

       if (isInvalid(user.getPassword())) {

           this.addFieldError("password", "密码项不能为空");

           return INPUT;

       }

      

       String uname = userService.isLogin(user.getUid(), user.getPassword());

       if (uname != null) {

           Map session = ActionContext.getContext().getSession();

           session.put("uname", uname);

           session.put("uid", user.getUid());
           this.addActionMessage("登录成功!");
          
           return SUCCESS;

       } else {

           this.addFieldError("idorpassword", "登录ID或密码错误");

           return INPUT;

       }

    }

    private boolean isInvalid(String value) {

       return (value == null || value.length() == 0);

    }

}

9.bean

package ssh2.news.vo;

public class User {

    private String uid;

    private String uname;

private String password;

public String getPassword() {
   return password;
}

10. Hibernate 的 xml 文件 User.hbm.xml

<?xml version="1.0"?>
<!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 - Hibernate Tools
-->
<hibernate-mapping>
    <class name="ssh2.news.vo.User" table="user" catalog="test">
        <id name="uid" type="java.lang.String">
            <column name="uid" length="50" />
            <generator class="native" />
        </id>
        <property name="uname" type="java.lang.String">
            <column name="uname" length="50" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="50" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

11.Dao接口类

package ssh2.news.dao;

import java.util.List;

import ssh2.news.vo.User;

public interface UserDao {

    /**

     *增加一个用户

     *@throwsException

     */

    public void addUser(User user) throws Exception;

    /**

     *根据uid/password查询User

     *@paramuid

     *@parampassword

     *@return

     *@throwsException

     */

    public User queryByUidAndPassword(String uid, String password) throws Exception;

    /**

     *删除用户

     *@paramuid

     *@throwsException

     */

    public void delete(String uid) throws Exception;

    /**

     *查询全部用户

     *@returnList

     *@throwsException

     */

    public List<User> queryAll() throws Exception;

}
12.DAO 实现类

package ssh2.news.dao.impl;

import java.util.List;

import ssh2.news.dao.UserDao;

import ssh2.news.vo.User;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**

* 继承HibernateDaoSuppor类实现getHibernateTemplate()

*

* @author bulktree

*

*/

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {


//   @Override

    public void addUser(User user) throws Exception {

       this.getHibernateTemplate().save(user);

    }

// @Override

    public void delete(String uid) throws Exception {

       // TODO Auto-generated method stub

    }

//    @Override

    public List<User> queryAll() throws Exception {

       // TODO Auto-generated method stub

       return null;

    }

//    @Override

    public User queryByUidAndPassword(String uid, String password)

           throws Exception {

       String hql = "FROM User as u WHERE u.uid=? and u.password=?";

       String[] str = new String[] { uid, password };

       List<User> users = this.getHibernateTemplate().find(hql, str);

       if (users != null && users.size() >= 1) {

           return users.get(0);

       } else {

           return null;

       }

    }


}
13.Service接口类

package ssh2.news.service;

public interface UserService {

    /**

     *添加一个用户

     *@paramuid

     *@paramuname

     *@parampassword

     *@return新增用户的uid

     *@throwsException

     */

    public String addUser(String uid, String uname, String password) throws Exception;

    /**

     *验证登录

     *@paramuid

     *@parampassword

     *@returnuid

     *@throwsException

     */

    public String isLogin(String uid, String password) throws Exception;

}


14.Service实现类

package ssh2.news.service.impl;

import ssh2.news.dao.UserDao;

import ssh2.news.service.UserService;

import ssh2.news.vo.User;

/**

* UserService实现类

*

* @author bulktree

*

*/

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {

       this.userDao = userDao;

    }

//   @Override

    public String addUser(String uid, String uname, String password)

           throws Exception {

       User user = new User();

       user.setUid(uid);

       user.setUname(uname);

       user.setPassword(password);

       userDao.addUser(user);

       return user.getUid();

    }

// @Override

    public String isLogin(String uid, String password) throws Exception {

       User user = userDao.queryByUidAndPassword(uid, password);

       if(user != null) {

           return user.getUname();

       } else {

           return null;

       }

    }

}
15.数据库脚本(mysql 5.0)

CREATE TABLE `user` (
`uid` varchar(50) NOT NULL,
`uname` varchar(50) default NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

 

 

备注:此文转载自http://hi.baidu.com/gaoshan1919/blog/item/d0c595b332e38ea2d8335a05.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics