首页 > 作文

idea搭建ssh框架的超详细教程

更新时间:2023-04-06 04:11:48 阅读: 评论:0

一。创建项目

先附上测试的数据库

drop table if exists `ur`;create table `ur` (  `id` int(5) not null auto_increment,  `name` varchar(10) collate utf8_bin default null,  `password` varchar(什么叫帽子戏法10) collate utf8_bin default null,  `remark` varchar(50) collate utf8_bin default null,  primary key (`id`)) engine=innodb auto_increment=5 default chart=utf8 collate=utf8_bin;-- ------------------------------ records of ur-- ----------------------------inrt into `ur` values ('1', 'zhangsan', '123456', null);inrt into `ur` values ('2', 'lisi', '123', null);inrt into `ur` values ('3', 'wangan', '456', null);inrt into `ur` values ('4', 'xinxi', '000', null);

1.new->project出现如下

点击next后出现如下填写groupid和artifactid在点击next直至finish

2.构建目录结构

在main下新建java和resources目录如下并将java目录标记为sources root,resources标记为resources root

在java下新建如下包package

二。struts2

1.首先引入struts2依赖

注:如果引入所有(spring和hibernate)依赖则在struts.xml中的对应action的类class就会找不到

<dependency>            <groupid>org.apache.struts</groupid>            <artifactid>struts2-core</artifactid>            <version>2.5.1</version>        </dependency>        <dependency>            <groupid>org.apache.struts</groupid>            <artifactid>struts2-json-plugin</artifactid>            <version>2.5.1</version>            <scope>compile</scope>        </dependency>

2.web-inf下web.xml文件配置

<?xml version="1.0" encoding="utf-8"?><web-app xmlns:xsi="/d/file/titlepic/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>archetype created web application</display-name>  <!--hibernate open ssion in view filte -->  <!-- <filter>      <filter-name>hibernatefilter</filter-name>      <filter-class>org.springframework.orm.hibernate3.support.openssioninviewfilter</filter-class>      singlession默认为true,若设为fal则等于没用openssioninview      <init-param>          <param-name>singlession</param-name>          <param-value>true</param-value>      </init-param>  </filter>  <filter-mapping>      <url-pattern>/*</url-pattern>  </filter-mapping> -->  <filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.filter.strutsprepareandexecutefilter</filter-class>    <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

3.在resources下添加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>    <!-- struts。xml的配置文件 -->    <!-- 所有匹配*.action的请求都由struts2处理 -->    <constant name="struts.action.extension" value="action,," />    <!-- 是否启用开发模式 -->    <constant name="struts.devmode" value="true" />    <package name="default" extends="struts-default" namespace="/">        <!-- 如果不指定method,那么默认方法是execute-->          <!--name:action类的名字,具有唯一性,页面上通过name来访问action类 -->        <action name="test" class="com.well.liu.action.testaction" method="test">              <!-- 配置结果集 :name属性的值一定要与execute方法的返回值一致-->            <result name="test">test.jsp</result>        </action>    </package></struts>

4.在action包下创建testaction类,为了测试在webapp下新建了test.jsp页面

package com.well.liu.action;import com.opensymphony.xwork2.actionsupport;public class testaction extends actionsupport {    public string test(){        system.out.println("测试struts2");        return "test";    }}

5.部署

点击如下图

点击+号找到local

接下来修改名字,选择deployment点击+号,光棍一条选择ssh:war exploded,另外application context填写的如果是ssh,那么访问的时候就是localhost:8080/ssh,部署结束。

6.启动测试

启动后默认访问的是index.jsp页面,可以访问http://localhost:8080/ssh/test

如果页面乱码在头部加上<%@ page language=̶高考电影1;java” contenttype=”text/html; chart=utf-8″ pageencoding=”utf-8″%>

三。spring

1.首先引入spring相关依赖

<dependency>            <groupid>org.springframework</groupid>            <artifactid>spring-context</artifactid>            <version>3.0.6.relea</version>        </dependency>        <dependency>            <groupid>org.springframework</groupid>            <artifactid>spring-context-support</artifactid>            <version>3.0.6.relea</version>        </dependency>        <dependency>            <groupid>org.springframework</groupid>            <artifactid>spring-webmvc</artifactid>            <version>3.0.6.relea</version>        </dependency>        <dependency>            <groupid>org.springframework</groupid>            <artifactid>spring-jdbc</artifactid>            <version>3.0.6.relea</version>        </dependency>        <dependency>            <groupid>org.springframework</groupid>            <artifactid>spring-orm</artifactid>            <version>3.0.6.relea</version>        </dependency>        <dependency>            <groupid>org.springframework</groupid>            <artifactid>spring-tx</artifactid>            <version>3.0.6.relea</version>        </dependency>        <dependency>            <groupid>org.aspectj</groupid>            <artifactid>aspectjweaver</artifactid>            <version>1.8.4</version>        </dependency>        <dependency>            <groupid>commons-logging</groupid>            <artifactid>commons-logging</artifactid>            <version>1.2</version>        </dependency>        <dependency>            <groupid>org.apache.struts</groupid>            <artifactid>struts2-spring-plugin</artifactid>            <version>2.5.1</version>        </dependency>

2.在web.xml中添加listener,并在resources下新建spring,xml文件

<listener>          <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>     </listener>    <context-param>  <param-name>contextconfiglocation</param-name>  <param-value>classpath:spring.xml</param-value></context-param> 

spring.xml文件

<?xml version="1.0" encoding="utf-8"?><beans xmlns="/d/file/titlepic/"    xmlns:xsi="/d/file/titlepic/xmlschema-instance" xmlns:context="/d/file/titlepic/"    xmlns:mvc="/d/file/titlepic/" xmlns:aop="/d/file/titlepic/"    xmlns:tx="/d/file/titlepic/" xmlns:amq="/d/file/titlepic/"    xmlns:task="/d/file/titlepic/"    xsi:schemalocation="http://www.springframework.org/schema/beans /d/file/titlepic/spring-beans-4.0.xsd        http://www.springframework.org/schema/context /d/file/titlepic/spring-context-4.0.xsd        http://www.springframework.org/schema/mvc /d/file/titlepic/spring-mvc-4.0.xsd        http://www.springframework.org/schema/tx /d/file/titlepic/spring-tx-4.0.xsd        http://www.springframework.org/schema/aop /d/file/titlepic/spring-aop-4.0.xsd        http://activemq.apache.org/schema/core /d/file/titlepic/activemq-core.xsd        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">    <bean id="test" class="com.well.liu.action.testaction">    </bean></beans>

如果你的spring.xml文件uri地址出错,解决点击菜单栏“file”→“ttings”→“languages&frameworks”

将之前的struts.xml中的<action name=”test” class=”com.well.liu.action.testaction” method=”test”>class改为”test”,也就是说与spring.xml中bean的id相对应,然后运行即可

四。hibernate

1.引入hibernate依赖

 <dependency>            <groupid>org.hibernate</groupid>            <artifactid>hibernate-core</artifactid>            <version>3.6.5.final</version> 亚州人体艺术       </dependency>        <dependency>            <groupid>mysql</groupid>            <artifactid>mysql-connector-java</artifactid>            <version>5.1.34</version>        </dependency>        <dependency>            <groupid>commons-dbcp</groupid>            <artifactid>commons-dbcp</artifactid>            <version>1.4</version>        </dependency>

2.view–》tool windows—->databa

找到所需连接的数据库

依次填写如下即可

右键点击项目添加support支持

找到hibernate并且打勾添加import databa schema打勾 含义:导入数据库模式

填写如下信息(数据源、实体生成包model位置、勾选需要生成的实体、勾选右下角最下面的两个按钮)即可生成实体和映射文件

3.编写代码

spring.xml文件

<?xml version="1.0" encoding="utf-8"?><beans xmlns="/d/file/titlepic/"    xmlns:xsi="/d/file/titlepic/xmlschema-instance" xmlns:context="/d/file/titlepic/"    xmlns:mvc="/d/file/titlepic/" xmlns:aop="/d/file/titlepic/"    xmlns:tx="/d/file/titlepic/" xmlns:amq="/d/file/titlepic/"    xmlns:task="/d/file/titlepic/"    xsi:schemalocation="http://www.springframework.org/schema/beans /d/file/titlepic/spring-beans-4.0.xsd        http://www.springframework.org/schema/context /d/file/titlepic/spring-context-4.0.xsd        http://www.springframework.org/schema/mvc /d/file/titlepic/spring-mvc-4.0.xsd        http://www.springframework.org/schema/tx /d/file/titlepic/spring-tx-4.0.xsd        http://www.springframework.org/schema/aop /d/file/titlepic/spring-aop-4.0.xsd        http://activemq.apache.org/schema/core /d/file/titlepic/activemq-core.xsd        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">高考必备;    <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource">        <property name="driverclassname" value="com.mysql.jdbc.driver" />        <property name="url" value="jdbc:mysql://localhost:3306/ur?characterencoding=utf-8" />        <property name="urname" value="root" />        <property name="password" value="root" />    </bean>    <!--ssionfactory配置-->    <bean id="ssionfactory" class="org.springframework.orm.hibernate3.localssionfactorybean" destroy-method="destroy">        <property name="datasource" ref="datasource"/>        <property name="mappingresources">            <list>                <value>com/well/liu/model/urentity.hbm.xml</value>            </list>        </property>        <property name="hibernateproperties">            <props>                <prop key="hibernate.dialect">org.hibernate.dialect.mysql5dialect</prop>                <prop key="hibernate.show_sql">fal</prop>            </props>    <bean id="urdao" class="com.well.liu.dao.urdao">        <property name="ssionfactory" ref="ssionfactory"></property>    <bean id="test" class="com.well.liu.action.testaction">        <property name="urrvice" ref="urrvice"></property>    <bean id="urrvice" class="com.well.liu.rvice.impl.urrviceimpl">        <property name="urdao" ref="urdao"></property>    <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">        <property name="datasource" ref="datasource"></property>    <!-- 定义事务通知 -->    <tx:advice id="txadvice" transaction-manager="transactionmanager">        <!-- 定义方法的过滤规则 -->        <tx:attributes>            <!-- 所有方法都使用事务 -->            <tx:method name="*" propagation="required"/>        </tx:attributes>    </tx:advice>    <!-- 定义aop配置 -->    <aop:config>        <!-- 定义一个切入点 -->        <aop:pointcut expression="execution (* com.well.liu.rvice.*.*(..))" id="rvices"/>        <!-- 对切入点和事务的通知,进行适配 -->        <aop:advisor advice-ref="txadvice" pointcut-ref="rvices"/>    </aop:config></beans>

在包dao下新建urdao

package com.well.liu.dao;import com.well.liu.model.urentity;import org.springframework.orm.hibernate3.support.hibernatedaosupport;public class urdao extends hibernatedaosupport {    public urentity getur(int id){        return this.gethibernatetemplate().get(urentity.class,id);    }}

在包rvice下新建urrvice

package com.well.liu.rvice;import com.well.liu.model.urentity;public interface urrvice {    public urentity getur(int id);}

在包impl下新建urrviceimpl

package com.well.liu.rvice.impl;import com.well.liu.dao.urdao;import com.well.liu.model.urentity;import com.well.liu.rvice.urrvice;public class urrviceimpl implements urrvice {    private urdao urdao;    public urdao geturdao() {        return urdao;    }    public void turdao(urdao urdao) {        this.urdao = urdao;    @override    public urentity getur(int id) {        urentity urentity = urdao.getur(id);        return urentity;}

testaction类

package com.well.liu.action;import com.opensymphony.xwork2.actionsupport;import com.well.liu.model.urentity;import com.well.liu.rvice.urrvice;public class testaction extends actionsupport {    private urrvice urrvice;    public urrvice geturrvice() {        return urrvice;    }    public void turrvice(urrvice urrvice) {        this.urrvice = urrvice;    public string test(){        urentity urentity = urrvice.getur(1);        system.out.println(urentity);        return "test";}

启动后会报如下错误

在网上找了资料将classpath改成classpath*、在pom中增加配置都还是同样的问题

最后将urentity.hbm.xml移动到resources下启动成功后后访问http://localhost:8080/ssh/test,出现地址则成功

到此这篇关于idea搭建ssh框架(超详细)的文章就介绍到这了,更多相关idea搭建ssh框架内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-06 04:11:24,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/8c1406749507cd8ac5210d0ae495aa3a.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:idea搭建ssh框架的超详细教程.doc

本文 PDF 下载地址:idea搭建ssh框架的超详细教程.pdf

标签:文件   定义   页面   测试
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图