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

网友投稿 1496 2022-10-20

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

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

目录一。创建项目1.new->project出现如下2.构建目录结构二。Struts21.首先引入struts2依赖2.WEB-INF下web.xml文件配置3.在resources下添加struts.xml文件4.在action包下创建TestAction类,为了测试在webapp下新建了test.jsp页面5.部署6.启动测试 三。Spring1.首先引入spring相关依赖2.在web.xml中添加listener,并在resources下新建spring,xml文件四。Hibernate1.引入hibernate依赖2.View--》Toolwindows---->Database3.编写代码

一。创建项目

先附上测试的数据

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (

`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 CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------

-- Records of user

-- ----------------------------

INSERT INTO `user` VALUES ('1', 'zhangsan', '123456', null);

INSERT INTO `user` VALUES ('2', 'lisi', '123', null);

INSERT INTO `user` VALUES ('3', 'wangan', '456', null);

INSERT INTO `user` 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就会找不到

org.apache.struts

struts2-core

2.5.1

org.apache.struts

struts2-json-plugin

2.5.1

compile

2.WEB-INF下web.xml文件配置

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">

Archetype Created Web Application

struts2

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

/*

</web-app>

3.在resources下添加struts.xml文件

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

test.jsp

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="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

三。Spring

1.首先引入spring相关依赖

org.springframework

spring-context

3.0.6.RELEASE

org.springframework

spring-context-support

3.0.6.RELEASE

org.springframework

spring-webmvc

3.0.6.RELEASE

org.springframework

spring-jdbc

3.0.6.RELEASE

org.springframework

spring-orm

3.0.6.RELEASE

org.springframework

spring-tx

3.0.6.RELEASE

org.aspectj

aspectjweaver

1.8.4

commons-logging

commons-logging

1.2

org.apache.struts

struts2-spring-plugin

2.5.1

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

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:spring.xml

spring.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:amq="http://activemq.apache.org/schema/core"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd

http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.0.xsd">

如果你的spring.xml文件uri地址出错,解决点击菜单栏“File”→“Settings”→“Languages&Frameworks”

将之前的struts.xml中的class改为"test",也就是说与spring.xml中bean的id相对应,然后运行即可

四。Hibernate

1.引入hibernate依赖

org.hibernate

hibernate-core

3.6.5.Final

mysql

mysql-connector-java

5.1.34

commons-dbcp

commons-dbcp

1.4

2.View--》Tool windows---->Database

找到所需连接的数据库

依次填写如下即可

右键点击项目添加support支持

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

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

3.编写代码

spring.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:amq="http://activemq.apache.org/schema/core"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd

http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.0.xsd">

com/well/liu/model/UserEntity.hbm.xml

org.hibernate.dialect.MySQL5Dialect

false

在包dao下新建UserDao

package com.well.liu.dao;

import com.well.liu.model.UserEntity;

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

public class UserDao extends HibernateDaoSupport {

public UserEntity getUser(int id){

return this.getHibernateTemplate().get(UserEntity.class,id);

}

}

在包service下新建UserService

package com.well.liu.service;

import com.well.liu.model.UserEntity;

public interface UserService {

public UserEntity getUser(int id);

}

在包impl下新建UserServiceImpl

package com.well.liu.service.impl;

import com.well.liu.dao.UserDao;

import com.well.liu.model.UserEntity;

import com.well.liu.service.UserService;

public class UserServiceImpl implements UserService {

private UserDao userDao;

public UserDao getUserDao() {

return userDao;

}

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

@Override

public UserEntity getUser(int id) {

UserEntity userEntity = userDao.getUser(id);

return userEntity;

}

TestAction类

package com.well.liu.action;

import com.opensymphony.xwork2.ActionSupport;

import com.well.liu.model.UserEntity;

import com.well.liu.service.UserService;

public class TestAction extends ActionSupport {

private UserService userService;

public UserService getUserService() {

return userService;

}

public void setUserService(UserService userService) {

this.userService = userService;

public String test(){

UserEntity userEntity = userService.getUser(1);

System.out.println(userEntity);

return "test";

}

启动后会报如下错误

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

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

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">

Archetype Created Web Application

struts2

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

/*

</web-app>

3.在resources下添加struts.xml文件

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

test.jsp

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="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

三。Spring

1.首先引入spring相关依赖

org.springframework

spring-context

3.0.6.RELEASE

org.springframework

spring-context-support

3.0.6.RELEASE

org.springframework

spring-webmvc

3.0.6.RELEASE

org.springframework

spring-jdbc

3.0.6.RELEASE

org.springframework

spring-orm

3.0.6.RELEASE

org.springframework

spring-tx

3.0.6.RELEASE

org.aspectj

aspectjweaver

1.8.4

commons-logging

commons-logging

1.2

org.apache.struts

struts2-spring-plugin

2.5.1

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

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:spring.xml

spring.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:amq="http://activemq.apache.org/schema/core"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd

http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:amq="http://activemq.apache.org/schema/core"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd

http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.0.xsd">

如果你的spring.xml文件uri地址出错,解决点击菜单栏“File”→“Settings”→“Languages&Frameworks”

将之前的struts.xml中的class改为"test",也就是说与spring.xml中bean的id相对应,然后运行即可

四。Hibernate

1.引入hibernate依赖

org.hibernate

hibernate-core

3.6.5.Final

mysql

mysql-connector-java

5.1.34

commons-dbcp

commons-dbcp

1.4

2.View--》Tool windows---->Database

找到所需连接的数据库

依次填写如下即可

右键点击项目添加support支持

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

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

3.编写代码

spring.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:amq="http://activemq.apache.org/schema/core"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd

http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.0.xsd">

com/well/liu/model/UserEntity.hbm.xml

org.hibernate.dialect.MySQL5Dialect

false

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:amq="http://activemq.apache.org/schema/core"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd

http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.0.xsd">

com/well/liu/model/UserEntity.hbm.xml

org.hibernate.dialect.MySQL5Dialect

false

在包dao下新建UserDao

package com.well.liu.dao;

import com.well.liu.model.UserEntity;

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

public class UserDao extends HibernateDaoSupport {

public UserEntity getUser(int id){

return this.getHibernateTemplate().get(UserEntity.class,id);

}

}

在包service下新建UserService

package com.well.liu.service;

import com.well.liu.model.UserEntity;

public interface UserService {

public UserEntity getUser(int id);

}

在包impl下新建UserServiceImpl

package com.well.liu.service.impl;

import com.well.liu.dao.UserDao;

import com.well.liu.model.UserEntity;

import com.well.liu.service.UserService;

public class UserServiceImpl implements UserService {

private UserDao userDao;

public UserDao getUserDao() {

return userDao;

}

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

@Override

public UserEntity getUser(int id) {

UserEntity userEntity = userDao.getUser(id);

return userEntity;

}

TestAction类

package com.well.liu.action;

import com.opensymphony.xwork2.ActionSupport;

import com.well.liu.model.UserEntity;

import com.well.liu.service.UserService;

public class TestAction extends ActionSupport {

private UserService userService;

public UserService getUserService() {

return userService;

}

public void setUserService(UserService userService) {

this.userService = userService;

public String test(){

UserEntity userEntity = userService.getUser(1);

System.out.println(userEntity);

return "test";

}

启动后会报如下错误

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

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[leetcode] 547. Friend Circles
下一篇:Symfony高性能全栈PHP框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~