SSH三大框架合辑的搭建步骤

时间:2021-11-27 14:31:38

v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}

Normal
0
false

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-priority:59;
mso-style-unhide:no;
border:solid black 1.0pt;
mso-border-themecolor:text1;
mso-border-alt:solid black .5pt;
mso-border-themecolor:text1;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid black;
mso-border-insideh-themecolor:text1;
mso-border-insidev:.5pt solid black;
mso-border-insidev-themecolor:text1;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

SSH三大框架合辑的搭建步骤

1.新建一个Java Web项目(testSSH)

2.导入三大框架,严格按照Struts - Hibernate - Spring的顺序。

1)导入Struts

选中项目testSSH,右键—MyEclipse—Add Struts Capabilities…—选择Struts2.1,URL pattern 选择“/*”—Next 下一步,为 Struts 2 Libraries选择:(除了其默认的一个Struts 库外)Struts 2 Spring Libraries -<MyEclipse-Library>勾选(因为Struts 要和Spring配合使用)—finish后生成struts.xml

SSH三大框架合辑的搭建步骤

2)导入Hibernate

  选中项目testSSH,右键—MyEclipse—Add Hibernate Capabilities…—选择Hibernate 3.2—下一步:选择DB Driver:oracle(已在DB视图配置好了)—下一步:Java Package 新建一个“com.sxl.factory”—finish后在该包下产生一个“HibernateSessionFactory.java”

3)导入Spring

  选中项目testSSH,右键—MyEclipse—Add Spring Capabilities…—选择Spring 3.0—勾选列表框第4个JDBC包(前四个都会被选中),下面Spring  Web Flow 2.0 Core要勾选—(此时下面两个会自动勾选,Hibernate3.2 Core..和Hibernate3.2 Annotations…)—Next—Finish

SSH三大框架合辑的搭建步骤  SSH三大框架合辑的搭建步骤

3.登录界面index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
…..
<body>
    登录界面:<br>
    <s:form action="login">
       <s:textfield label="name" name="student.name"></s:textfield><br>
       <s:password label="pass" name="student.pass"></s:password><br>
       <s:submit value="login"></s:submit>
       <s:reset value="reset"></s:reset>
    </s:form>
  </body>

4.文件目录结构如下:

SSH三大框架合辑的搭建步骤

5.在StudentAction.java重写Execute()方法

6.在strust.xml 配置文件中进行配置

SSH三大框架合辑的搭建步骤

SSH三大框架合辑的搭建步骤

7.pojos 的类生成的方式:

在DB视图-右击Student表—Hibernate Reverse Engineering反转引擎

SSH三大框架合辑的搭建步骤

SSH三大框架合辑的搭建步骤

SSH三大框架合辑的搭建步骤

8.服务层StudentService.java

package com.sxl.service;

import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.sxl.pojos.Student;

public class StudentService {
     private HibernateTemplate template;
    //为了利用注入的方式读取模板
    public HibernateTemplate getTemplate() {
        return template;
    }

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }
    //验证用户登录
    public boolean CheckUser(Student s) {
        //查找
        List ls=template.find("from Student where name='"+s.getName()+"' and pass='"+s.getPass()+"'");
        System.out.println("添加用户:"+s.getName()+","+s.getPass());

        if (ls.size()>0) {
            return true;
        }
        return false;
    }
}

9.在StudentAction.Java中调用上面的服务层

package com.sxl.action;

import com.opensymphony.xwork2.ActionSupport;
import com.sxl.pojos.Student;
import com.sxl.service.StudentService;

public class StudentAction extends ActionSupport {
    private Student student;
    private StudentService service;

    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public StudentService getService() {
        return service;
    }
    public void setService(StudentService service) {
        this.service = service;
    }
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if (service.CheckUser(student)) {
            return "success";
        }
        return "failed";
    }
}

10.在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="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"
            value="classpath:hibernate.cfg.xml">
        </property>
    </bean>
    <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
       <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="stu" class="com.sxl.pojos.Student"></bean>
    <bean id="ss" class="com.sxl.service.StudentService">
           <property name="template" ref="template"></property>
    </bean>
    //
    <bean id="logins" class="com.sxl.action.StudentAction">
         <property name="student" ref="stu"></property>
         <property name="service" ref="ss"></property>
    </bean>

    </beans>

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="works" namespace="/" extends="struts-default">
        <action name="login" class="logins">
  这里的class原先是自己引用的类名com.sxl.action.StudentAction,要改为logins
            <result name="success" type="dispatcher">
                /success.jsp
            </result>
            <result name="failed" type="redirect">/failed.jsp</result>
        </action>
    </package>
</struts>    

第一次运行出错,

SSH三大框架合辑的搭建步骤

原因是:

Spring在使用的时候需要在web.xml配置文件中对<listener>进行注册,否则代码运行会报错。如下:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> 

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

亦可直接拷贝web.xml文件。

测试结果:

SSH三大框架合辑的搭建步骤         SSH三大框架合辑的搭建步骤

v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}

SSH三大框架合辑的搭建步骤

1.新建一个Java Web项目(testSSH)

2.导入三大框架,严格按照Struts - Hibernate - Spring的顺序。

1)导入Struts

选中项目testSSH,右键—MyEclipse—Add Struts Capabilities…

—选择Struts2.1,URL pattern 选择“/*”—Next 下一步,为 Struts 2 Libraries选择:(除了其默认的一个Struts 库外)Struts 2 Spring Libraries -<MyEclipse-Library>勾选(因为Struts 要和Spring配合使用)—finish后生成struts.xml

SSH三大框架合辑的搭建步骤

2)导入Hibernate

选中项目testSSH,右键—MyEclipse—Add Hibernate Capabilities…

—选择Hibernate 3.2—下一步:选择DB Driver:oracle(已在DB视图配置好了)—下一步:Java Package 新建一个“com.sxl.factory”

—finish后在该包下产生一个“HibernateSessionFactory.java”

3)导入Spring

选中项目testSSH,右键—MyEclipse—Add Spring Capabilities…

—选择Spring 3.0—勾选列表框第4个JDBC包(前四个都会被选中),下面Spring  Web Flow 2.0 Core要勾选—(此时下面两个会自动勾选,Hibernate3.2 Core..和Hibernate3.2 Annotations…)—Next—Finish

SSH三大框架合辑的搭建步骤

SSH三大框架合辑的搭建步骤

3.登录界面index.jsp

<%@ taglib
prefix="s" uri="/struts-tags" %>

…..

<body>

登录界面:<br>

<s:form action="login">

<s:textfield label="name" name="student.name"></s:textfield><br>

<s:password label="pass" name="student.pass"></s:password><br>

<s:submit value="login"></s:submit>

<s:reset value="reset"></s:reset>

</s:form>

</body>

4.文件目录如下:

SSH三大框架合辑的搭建步骤

5.在StudentAction.java重写Execute()方法

6.在strust.xml 配置文件中进行配置

SSH三大框架合辑的搭建步骤

SSH三大框架合辑的搭建步骤

7.pojos 的类生成的方式:

在DB视图-右击Student表—Hibernate Reverse Engineering反转引擎

SSH三大框架合辑的搭建步骤

SSH三大框架合辑的搭建步骤

SSH三大框架合辑的搭建步骤

8.服务层StudentService.java

package com.sxl.service;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.sxl.pojos.Student;

publicclass StudentService {

private HibernateTemplate template;

//为了利用注入的方式读取模板

public
HibernateTemplate getTemplate() {

returntemplate;

}

publicvoid
setTemplate(HibernateTemplate template) {

this.template = template;

}

//验证用户登录

publicboolean
CheckUser(Student s) {

//查找

List
ls=template.find("from Student where name='"+s.getName()+"' and
pass='"+s.getPass()+"'");

System.out.println("添加用户:"+s.getName()+","+s.getPass());

if (ls.size()>0) {

returntrue;

}

returnfalse;

}

}

9.在StudentAction.Java中调用上面的服务层

package com.sxl.action;

import com.opensymphony.xwork2.ActionSupport;

import com.sxl.pojos.Student;

import com.sxl.service.StudentService;

publicclass StudentAction extends ActionSupport {

private Student student;

private StudentService service;

public Student getStudent() {

returnstudent;

}

publicvoid setStudent(Student student) {

this.student = student;

}

public StudentService getService() {

returnservice;

}

publicvoid setService(StudentService
service) {

this.service = service;

}

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

if (service.CheckUser(student)) {

return"success";

}

return"failed";

}

}

10.在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="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="configLocation"

value="classpath:hibernate.cfg.xml">

</property>

</bean>

<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">

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

</bean>

<bean id="stu" class="com.sxl.pojos.Student"></bean>

<bean id="ss" class="com.sxl.service.StudentService">

<property name="template"
ref="template"></property>

</bean>

//

<bean id="logins" class="com.sxl.action.StudentAction">

<property name="student"
ref="stu"></property>

<property name="service"
ref="ss"></property>

</bean>

</beans>

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="works"
namespace="/" extends="struts-default">

<action name="login" class="logins">

这里的class原先是自己引用的类名com.sxl.action.StudentAction,不行,要logins

<result name="success" type="dispatcher">

/success.jsp

</result>

<result name="failed" type="redirect">/failed.jsp</result>

</action>

</package>

</struts>

第一次运行出错,

SSH三大框架合辑的搭建步骤

原因是:

Spring在使用的时候需要在web.xml配置文件中对<listener>进行注册,否则代码运行会报错。如下:

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

亦可直接拷贝web.xml文件。

测试结果:

SSH三大框架合辑的搭建步骤

SSH三大框架合辑的搭建步骤

Normal
0
false

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-priority:59;
mso-style-unhide:no;
border:solid black 1.0pt;
mso-border-themecolor:text1;
mso-border-alt:solid black .5pt;
mso-border-themecolor:text1;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid black;
mso-border-insideh-themecolor:text1;
mso-border-insidev:.5pt solid black;
mso-border-insidev-themecolor:text1;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}