`

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

分享到:
评论

相关推荐

    struts2.0.11 jar 包

    Struts2.0.11是一个重要的Java Web框架,它基于Apache Struts 2的核心版本,旨在提供一个灵活且强大的MVC(Model-View-Controller)架构,用于构建可维护性和扩展性良好的企业级Web应用。这个框架是在原有的WebWork...

    struts2-core.jar

    struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar, struts2-core-2.0.8.jar, struts2-core-2.0.9.jar, struts2-core-2.1.2.jar, ...

    struts2.0.11.rar

    这个压缩包"struts2.0.11.rar"包含了用于构建Struts2应用程序的必要组件,总计40个框架相关的文件。以下是其中一些关键组件的详细解释: 1. **struts2-core-2.0.11.jar**:这是Struts2的核心库,包含Action、...

    SSHF备忘之依赖包(struts2+spring2.5+hibernate+freemarker)

    依赖包包括`struts2-core-2.0.11.jar`和`struts2-spring-plugin-2.0.11.jar`,后者用于将Struts2与Spring集成。 2. **Spring2.5**:Spring框架是Java企业级应用的核心框架,提供了依赖注入、面向切面编程、事务管理...

    struts2.0.11类包

    struts2.0.11类包。一般用下面的五个类包就够用了。xwork-2.0.4,commons-logging-api-1.1,freemarker-2.3.8,ognl-2.6.11,struts2-core-2.0.11

    Spring2.5.3+Hibernate3.2+Struts2.0.11整合.

    这个整合项目中的文档,如"Spring2.5+Hibernate3.2+Struts2.0+jotm2.0集成配置.doc"、"spring 2.0 + hibernate3.2 + struts 2.0 开放配置.pdf"和"Spring2.5.3+Hibernate3.2+Struts2.0.11整合.pdf"应该详细介绍了这些...

    Myeclipse+struts2.0.11+hibernate+spring配置

    在本文中,我们将深入探讨如何在Myeclipse环境中配置一个基于Struts2.0.11、Hibernate和Spring的Web项目。这个配置过程分为几个关键步骤,确保所有必要的库和框架能够协同工作。 首先,我们需要在MyEclipse中创建一...

    struts2-core-2.0.11源码

    Struts2核心包`struts2-core-2.0.11`包含了框架的核心组件和功能,使得开发者能够构建结构清晰、易于维护的Web应用。 在源码分析中,以下几个关键知识点是非常重要的: 1. **拦截器(Interceptors)**:Struts2的...

    PyPI 官网下载 | plone.namedfile-2.0.11.tar.gz

    《PyPI官网下载:plone.namedfile-2.0.11.tar.gz——Python库解析与应用》 PyPI(Python Package Index),是Python开发者最重要的软件仓库,它提供了丰富的Python库,供全球开发者免费下载和使用。本次我们将关注...

    Spring2.5.3+Hibernate3.2+Struts2.0.11整合

    在本文中,我们将探讨如何将Spring 2.5.3、Hibernate 3.2和Struts 2.0.11这三大流行Java企业级框架进行整合。这种整合通常用于构建复杂的Web应用程序,提供数据持久化、业务逻辑管理以及用户界面控制。下面我们将...

    Struts2.0.11-lib

    1. `struts2-core-2.0.11.jar`:这是Struts2的核心库,包含了框架的主要功能和组件。 2. `struts2-convention-plugin-2.0.11.jar`:提供了基于约定优于配置的Action和结果自动映射功能。 3. `struts2-json-plugin-...

    struts2.0.11源代码

    Struts2.0.11源代码是Apache Struts框架的一个特定版本,它是一个流行的Java Web应用程序开发框架,用于构建MVC(Model-View-Controller)架构的应用程序。这个源代码包提供了对Struts2核心组件、拦截器、插件以及...

    struts-2.0.11-src.zip

    struts-2.0.11的jar文件 需要说明的是这个jar文件并不是...struts2-spring-plugin-2.0.11.jar, xwork-2.0.4.jar, commons-fileupload-1.1.1.jar, commons-logging-1.0.4.jar, 前五个为整合必须的,后两个并不必要

    struts-2.0.11

    Struts2.0.11是Apache软件基金会的开源项目Struts的一个版本,它是一个基于MVC(Model-View-Controller)设计模式的Java Web应用框架。Struts2旨在简化开发,提供一种处理HTTP请求、业务逻辑整合以及视图渲染的方法...

    JavaEE源代码 struts2-spring-plugin-2.0.11

    JavaEE源代码 struts2-spring-plugin-2.0.11JavaEE源代码 struts2-spring-plugin-2.0.11JavaEE源代码 struts2-spring-plugin-2.0.11JavaEE源代码 struts2-spring-plugin-2.0.11JavaEE源代码 struts2-spring-plugin-...

    struts-2.0.11-all.zip

    Struts2.0.11-all.zip 是一个包含Apache Struts 2.0.11版本的完整包,它是一个广泛使用的开源MVC框架,专为Java开发人员设计,用于构建可维护、易于扩展的企业级Web应用程序。Struts 2是Struts 1的升级版,提供了更...

    edu.northwestern.bioinformatics.osgi.casclient-2.0.11.jar

    jar包,官方版本,自测可用

    clipboard.js v2.0.11

    clipboard.js v2.0.11,支持复制功能

    Struts2 + Spring2.5 + JFreeChart 实现的投票系统,2008年高考作文评价投票

    1、开发环境:MyEclipse6.0, Struts 2.0.11, Spring2.5.4, struts2-spring-plugin-2.0.11.1,JFreeChart 1.0.9, JCommon 1.0.12,struts2-jfreechart-plugin-2.0.11.1,JDK1.5+ 2、下载地址: ...

    struts-2.3.30-all所有jar包

    mvel2-2.0.11.jar, ognl-3.0.19.jar, org.apache.felix.framework-4.0.3.jar, org.apache.felix.main-4.0.3.jar, org.apache.felix.shell-1.4.3.jar, org.apache.felix.shell.tui-1.4.1.jar, org.osgi.compendium-...

Global site tag (gtag.js) - Google Analytics