1,选择maven
2,选择project sdk 安装的是1.7版的jdk,如果是第一次安装idea 需要配置本地jdk
3,可以选择生成相应的项目结构,本次不作选择,直接下一步
maven项目必须填写GroupId 和ArtifactId,填写后直接下一步
1.填写项目名称 填写完成后点击完成
2.项目名称,目录地址
1.新建项目的目录结构
2.maven的pom文件,文件名称是项目名称,
3.groupId和artifactId ,这两个id命名通常是不同的有一定规则,这里为了方便,简单命名
1.maven project 可以看到有关maven 的功能项
2lifecyle 点击clean会将项目解除依赖 ,install会添加依赖
3.maven项目如果出现jar包更新,运行,必须进行点击clean,重新install。
进入到配置界面
1.点击加号添加tomcat server 如果没有tomcat 选项要在idea中安装tomcat插件具体在seting中。
2.选择local
1.填写名字
2.点击下拉框选择本地tomcat目录进行配置
3.部署
点击artifact进行部署,这里部署方式是将war包进行发布,如果新建项目没有war包可以点击右下方Fix进行生成,本次是使用maven进行打war包,具体在pom文件中进行配置。
war包发布后 点击Server,可以进行相关配置
至此新建maven项目完毕,剩下就应该是整合框架。
org.springframework
spring-core
3.2.3.RELEASE
org.springframework
spring-beans
3.2.3.RELEASE
org.springframework
spring-web
3.2.3.RELEASE
org.springframework
spring-context
3.2.3.RELEASE
org.springframework
spring-jdbc
3.2.3.RELEASE
org.springframework
spring-webmvc
3.2.3.RELEASE
org.springframework
spring-webmvc-portlet
3.2.3.RELEASE
1.在项目目录中创建spring.xml配置文件,idea工具可以自动生成配置文件,在项目根目录上右键添加框架支持。
2.初始spring配置文件,只要写入自动扫描包即可,在项目启动时spring会自动去扫描。
3.本项目采用注解方式,不再配置文件中在写相关配置。
1.编写service层和service实现类
2.注解方式,在spring启动的时会根据这个注解将它加入到上下文中,相当于在spring配置文件中加
因为在spring配置文件中写了自动扫描包,只要service文件能被扫描到就可以。
1.在test文件目录下建测试文件。
2.获取spring上下文
3.JUnit测试方法,不需要写main方法。
要让spring在web项目中应用,所以要在web.xml文件中配置。
1.在pom文件中加入struts2核心包,struts2的注解包,因为要和spring做整合,所以要加入struts2-spring-plugin。
2.idea中如果有个功能未自动导入jar包,就是在pom文件中写完jar包后会自动导入,右下方eventlog
3.点击enable,就可以自动导入jar包了
1.创建config目录,struts配置文件struts.xml,
2.web目录,web.xml文件
struts2基础配置文件struts.xml基本配置
因为struts2-spring-plugin 包spring就会自动找@Action将它加入spring进行管理
在浏览器中输入 http://localhost:8081/userAction!test.action 因为部署时没有写项目名称所以地址不用写项目名称
在spring中配置数据库连接池采用的是druid,数据库为oracle。
添加jar包
在spring中配置连接池。这里另外建立了一个spring-dataSource文件,在spring-config文件中加载这个文件即可,durid配置连接池为固定格式。将jdbc文件放在指定目录下载spring-datasource文件中指定即可
spring-datasource文件
在spring-config.xml文件中引入 spring-datasource.xml文件
建立dao层和Mapper层目录结构,mybatis有很多方式实现mapper和dao层对应,这里采用在Mapper中配置命名空间。
package com.ztx.dao;
import com.ztx.entity.User;
/**
* Created by Think on 2016/10/19.
*/
public interface UserDao {
User selectById(String id);
}
每添加一个dao,就要在spring-config中配置
package com.ztx.action;
import com.ztx.entity.User;
import com.ztx.service.UserService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import javax.annotation.Resource;
@ParentPackage("struts-default")
@Namespace("/") //命名空间为根目录
@Action(value = "userAction")
public class userAction {
@Resource(name = "userService")
private UserService userService;
public void test(){
System.out.println("进入了Action");
String id = "盖伦";
User u = userService.SelestPwdById(id);
System.out.println("pwd:"+u.getPwd());
}
}
package com.ztx.service;
import com.ztx.entity.User;
/**
* Created by Think on 2017/1/22.
*/
public interface UserService {
public void test();
public User SelestPwdById(String id);
}
package com.ztx.service.impl;
import com.ztx.dao.UserDao;
import com.ztx.entity.User;
import com.ztx.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Resource(name="userDao")
private UserDao userDao;
@Override
public void test() {
System.out.println("测试spring");
}
@Override
public User SelestPwdById(String id) {
return userDao.selectById(id);
}
}
package com.ztx.dao;
import com.ztx.entity.User;
/**
* Created by Think on 2016/10/19.
*/
public interface UserDao {
User selectById(String id);
}
package com.ztx.entity;
import java.util.Date;
public class User {
private String id;
private String name;
private String pwd;
private Date createDateName;
private Date modifyDateTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Date getCreateDateName() {
return createDateName;
}
public void setCreateDateName(Date createDateName) {
this.createDateName = createDateName;
}
public Date getModifyDateTime() {
return modifyDateTime;
}
public void setModifyDateTime(Date modifyDateTime) {
this.modifyDateTime = modifyDateTime;
}
}
TsetSpring.java
/*
import com.ztx.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
@Test
public void test(){
//获得这个spring-config。
ApplicationContext ac = new ClassPathXmlApplicationContext
(new String[]{"classpath:\\com\\ztx\\config\\spring\\spring-config.xml"});
UserService userService= (UserService) ac.getBean("userService"); //调用bean 强转
userService.test();
}
}
*/
配置文件
driver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@127.0.0.1:1521:xe
username= xhy
password= xhy
contextConfigLocation
classpath:com/ztx/config/spring/spring-config.xml
org.springframework.web.context.ContextLoaderListener
struts
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts
*.action
/index.jsp
4.0.0
import
import
war
1.0-SNAPSHOT
import Maven Webapp
http://maven.apache.org
org.apache.poi
poi
3.12
org.apache.poi
poi-ooxml
3.12
org.eclipse.birt.runtime.3_7_1
org.apache.xerces
2.9.0
org.apache.directory.studio
org.dom4j.dom4j
1.6.1
net.sourceforge.javacsv
javacsv
2.0
commons-net
commons-net
3.3
org.jodd
jodd-props
3.6.6
org.slf4j
slf4j-api
1.7.12
org.slf4j
slf4j-log4j12
1.7.12
com.jcraft
jsch
0.1.53
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
jsp-api
2.1
provided
com.google.code.gson
gson
2.5
commons-collections
commons-collections
3.2.1
junit
junit
4.12
org.aspectj
aspectjweaver
1.8.4
aopalliance
aopalliance
1.0
cglib
cglib-nodep
2.2.2
org.springframework
spring-core
3.2.3.RELEASE
org.springframework
spring-beans
3.2.3.RELEASE
org.springframework
spring-web
3.2.3.RELEASE
org.springframework
spring-context
3.2.3.RELEASE
org.springframework
spring-jdbc
3.2.3.RELEASE
org.springframework
spring-webmvc
3.2.3.RELEASE
org.springframework
spring-webmvc-portlet
3.2.3.RELEASE
org.apache.struts
struts2-core
2.3.4.1
org.apache.struts
struts2-convention-plugin
2.3.4.1
org.apache.struts
struts2-spring-plugin
2.3.4.1
junit
junit
4.12
test
org.mybatis
mybatis-spring
1.2.2
org.mybatis
mybatis
3.1.1
com.oracle
ojdbc6
1.0
com.alibaba
druid
1.0.14
aopalliance
aopalliance
1.0
cglib
cglib-nodep
2.2.2
import
src/main/java
**/*.sql
**/*.xml
false
src/main/resources
**/*.properties
**/*.xml
**/*.props
false
maven-compiler-plugin
2.3.2
1.7
maven-war-plugin
2.2
false