ssh用户登录

1.搭建环境,连数据库,建包建类

2.Admin,这是表,表必须有主码

package com.chao.db;

/**
 * Admin entity. @author MyEclipse Persistence Tools
 */

public class Admin implements java.io.Serializable {

    // Fields

    private String username;
    private String password;

    // Constructors

    /** default constructor */
    public Admin() {
    }

    /** minimal constructor */
    public Admin(String username) {
        this.username = username;
    }

    /** full constructor */
    public Admin(String username, String password) {
        this.username = username;
        this.password = password;
    }

    // Property accessors

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

3.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>

    <package name="aaa"  extends="struts-default">
        <action name="Login" class="LoginAction">
            <result name="success">/success.html</result>
            <result name="error">/error.html</result></action></package></struts>    

4.AdminDao和AdminDaoImp类,这是对表的查询

package com.chao.dao;

import com.chao.db.Admin;

public interface AdminDao {
    Admin find(String username,String password);
}
package com.chao.dao;

import java.util.List;

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

import com.chao.db.Admin;

public class AdminDaoImp extends HibernateDaoSupport implements AdminDao{

    @Override
    public Admin find(String username, String password) {
        // TODO Auto-generated method stub
        @SuppressWarnings("rawtypes")
        List ul = getHibernateTemplate().find("from Admin where username = '"+ username +"' and password='"+password+"'");
         
        if (ul.size() != 0)
        {
            return (Admin)ul.get(0);
        }
        return null;
    }

}

5.ActionManager和ActionManagerImp

package com.chao.service;

import java.util.List;

import com.chao.db.Admin;

public interface ActionManager {

    List<Admin> find(String username,String password); 
}
package com.chao.service;

import java.util.ArrayList;
import java.util.List;

import com.chao.dao.AdminDao;
import com.chao.db.Admin;



public class ActionManagerImp implements ActionManager{
    private AdminDao admindao;

    

    public AdminDao getAdmindao() {
        return admindao;
    }

    public void setAdmindao(AdminDao admindao) {
        this.admindao = admindao;
    }

    
    @Override
    public List<Admin> find(String username,String password) {
        // TODO Auto-generated method stub
                try {
                    Admin adminlist =admindao.find(username, password);
                    if (adminlist!= null)
                    {
                       List<Admin> list = new ArrayList<Admin>();
                       list.add(adminlist);                       
                       return list;
                    }
              }
              catch (Exception e)
              {
                     System.out.println(e.getMessage());         
              }   
                 return null;
    }
    

}

6.login.action

package com.chao.action;

import java.util.List;

import com.chao.db.Admin;
import com.chao.service.ActionManager;
import com.opensymphony.xwork2.ActionSupport;

public class login extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private Admin admin;
    protected ActionManager mgr;
    public ActionManager getMgr() {
        return mgr;
    }
    public void setMgr(ActionManager mgr) {
        this.mgr = mgr;
    }
    
    private String username="";
    private String password="";
    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() throws Exception {
        // TODO Auto-generated method stub
        String u=getUsername();
        String p=getPassword();
        
        List<Admin> aa = mgr.find(u, p);   
        
        if(aa!=null)
        {
            return "success";
        }
        
       else
       {
           return "error";
       }
           
    }
}

7.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>*.action</url-pattern>
  </filter-mapping>
   <!-- Spring配置与Struts2的监听 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  </web-app>

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


    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
        </property>
        <property name="url" value="jdbc:sqlserver://localhost"></property>
        <property name="username" value="sa"></property>
        <property name="password" value="1"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.SQLServerDialect
                </prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/chao/db/Admin.hbm.xml</value></list>
        </property></bean>
        
        <bean id="LoginAction" class="com.chao.action.login">
             <property name="mgr" ref="mgr"></property> 
        </bean>
        
         <bean id="mgr" class="com.chao.service.ActionManagerImp">
            <property name="admindao" ref="adminDao"></property>   
        </bean>
        
        <bean id="adminDao" class="com.chao.dao.AdminDaoImp">
            <property name="sessionFactory">
                <ref local="sessionFactory" />
            </property>
        </bean>
        
        </beans>

 

posted @ 2016-10-08 13:24  非非是  阅读(739)  评论(0编辑  收藏  举报