ssh的安装

来源:互联网 发布:sunrise软件 编辑:程序博客网 时间:2024/06/15 00:09

今天我们来介绍ssh的安装和使用,首先还是要配置环境

eclipse和tomcat的配置我已经在之前的博客里面讲过了,就不在赘述了,今天主要来将struts,spring和hibernate三个框架的介绍:


首先,打开eclipse,创建一个webproject,并选择tomcat的安装路径以及jre版本的选择。

然后首先把Struts2所需要的jar,复制到WebContent下的WEB-INF下的lib里面,然后配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>sshTest</display-name>
   <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>/*</url-pattern>
    </filter-mapping>
  <welcome-file-list>


    <welcome-file>index.jsp</welcome-file>


  </welcome-file-list>
</web-app>

之后,建立ActionSupport类。

package com.hy.action;


import com.opensymphony.xwork2.ActionSupport;


public class TestAction extends ActionSupport{
public String execute() throws Exception{
System.out.println("struts=========");
return "success";
}


}

然后我们在src下创建一个叫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="default" extends="struts-default">
            <action name="login" class="com.hy.action.TestAction">
            <result name="success">index.jsp</result>
        </action>
        </package>
    </struts>

然后再WebContent文件下创建一个index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
struts test
</body>
</html>

若运行后http://localhost:8080/sshTest/显示为struts test即为成功。

struts的配置到此结束。

下面进行spring和struts的整合:

创建applicationContext.xml,定义里面的内容为:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-3.0.xsd"><!-- Service配置 --><beanid="loginService"class="com.hy.service.impl.LoginServiceImpl" /><!-- Action配置 --><beanid="loginServer"class="com.hy.action.LoginAction"scope="prototype"><propertyname="loginService"ref="loginService"></property></bean></beans>

在web.xml中进行如下配置

<packagename="struts2"extends="struts-default"><!-- 此处的class的内容要与Spring配置文件中的bean的id相同 --><actionname="Login"class="loginServer"><resultname="success">/result.jsp</result><resultname="input">/login.jsp</result></action></package>

下面进行hibernate的整合:

在applicationContext.xml文件下添加如下配置:

<!-- 配置数据源 --><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><!-- 指定连接数据库的驱动 --><propertyname="driverClassName"value="com.mysql.jdbc.Driver" /><!-- 指定连接数据库的URL --><propertyname="url"value="jdbc:mysql://localhost:3306/test" /><!-- 指定连接数据库的用户名 --><propertyname="username"value="root" /><!-- 指定连接数据库的密码 --><propertyname="password"value="" /></bean><!-- 配置SessionFactory --><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="dataSource"><refbean="dataSource" /></property><propertyname="hibernateProperties"><props><propkey="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop><propkey="hibernate.hbm2ddl.auto">update</prop><propkey="hibernate.connection.autocommit">true</prop><propkey="hibernate.show_sql">true</prop><propkey="sql_format">true</prop></props></property><propertyname="mappingResources"><!-- 指定hibernate映射文件 --><list><value>com/hy/entity/User.hbm.xml</value></list></property></bean>

至此,ssh的配置完毕