项目管理工具Maven3

四,创建SSH2工程(重点)
需求,用SSH2集成框架搭建工程mavenweb,功能为单表的列表查询。
表名称dep(部门表)
项目管理工具Maven3
(一)创建战工程
1.选择菜单File - > new - > Other
项目管理工具Maven3
选择Maven下的Maven Project,接下来
2.选中下图红框部分的复选框(跳过骨架),接下来
项目管理工具Maven3

3.定义工程坐标
项目管理工具Maven3

注意打包方式选择战争工程。
4.手动添加web.xml
(1)在src / main / webapp目录下手动创建WEB-INF目录
(2)将资源文件夹下的web.xml中拷贝至WEB-INF目录
项目管理工具Maven3
5.添加插件

将下列配置拷贝至的pom.xml  
[mw_shl_code = AppleScript的,真<建立>  
  <plugins> 
      <plugin>   
          <groupId> org.apache.maven.plugins </ groupId>   
          <artifactId> maven-compiler-plugin </ artifactId>   
          <version> 2.3.2 </ version>   
          <configuration>   
              <source> 1.7 < / source>   
              <target> 1.7 </ target>   
          </ configuration>   
      </ plugin>   
      <plugin> 
<groupId> org.apache.tomcat.maven </ groupId> 
<artifactId> tomcat7-maven-plugin </ artifactId> 
<configuration > 
<! - 指定端口 - > 
<port> 8080 </ port> 
<! - 请求路径 - > 
<path> / </ path>
</ configuration> 
            </ plugin> 
  </ plugins>   
</ build> [/ mw_shl_code]

点击工程右键Maven - >更新项目...

  1. 在的pom.xml中添加servlet的API
    因为我们创建的是war工程,需要响应和请求等对象,而这些对象是在servlet-api中的,所以我们需要添加servlet-api,才能保证工程正常编译。
    注意:必须指定scope为provided,这是设置依赖范围,我们后边的章节会详细讲解。如果设置则启动会报错。
    [mw_shl_code = applescript,true] 
    <dependencies> 
          <dependency> 
          <groupId> javax.servlet </ groupId> 
          <artifactId> servlet-api </ artifactId> 
          <version> 2.5 </ version> 
    <scope> provided </ scope> 
          < / dependency> 
    </ dependencies> [/ mw_shl_code] 添加后更新工程

    7.创建index.jsp

    [mw_shl_code = AppleScript的,真] <%@页语言= “Java”的的contentType = “text / html的;字符集= UTF-8” 
    的pageEncoding = “UTF-8” %> 
    <!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 = UTF-8“> 
    <title>测试页面</ 
    head> 
    < / head> <body> 
    开启神秘的Maven世界〜
    </ body> 
    </ html> [/ mw_shl_code]

    部署运行测试执行命令
    tomcat7:运行
    用浏览器测试,地址:http:// localhost:8080 / index.jsp
    (二)添加SSH依赖
    SSH2框架需要添加的依赖如下:
    [mw_shl_code = applescript,true] <dependencies>
    <dependency>
    <groupId> javax.servlet </ groupId>
    <artifactId> servlet-api </ artifactId>
    <version> 2.5 </ version>
    <scope> provided </ scope>
    < / dependency>
    <dependency>
    <groupId> org.hibernate </ groupId>
    <artifactId> hibernate-core </ artifactId>
    <version> 5.0.7.Final </ version>
    </ dependency>
    <dependency>
    <groupId> org。 springframework </ groupId>
    <artifactId> spring-core </ artifactId>
    <version> 4.2.4.RELEASE </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId的>弹簧豆</ artifactId的>
    <版本> 4.2.4.RELEASE </版本>
    </依赖性>
    <依赖性>
    <的groupId> org.springframework </的groupId>
    <artifactId的>弹簧上下文</ artifactId的>
    < version> 4.2.4.RELEASE </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-orm </ artifactId>
    <version> 4.2.4.RELEASE </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId>spring-test </ artifactId>
    <version> 4.2.4.RELEASE </ version>
    </ dependency>
    <依赖性>
    <的groupId> org.springframework </的groupId>
    <artifactId的>弹簧上下文支持</ artifactId的>
    <版本> 4.2.4.RELEASE </版本>
    </依赖性>
    <依赖性>
    <的groupId> org.springframework </的groupId>
    <artifactId的>弹簧的AOP </ artifactId的>
    <版本> 4.2.4.RELEASE </版本>
    </依赖性>
    <依赖性>
    <的groupId> org.springframework </的groupId>
    <artifactId的>弹簧的web < / artifactId>
    <version> 4.2.4.RELEASE </ version>
    </ dependency>
    <dependency>
    <groupId> org.aspectj </ groupId>
    <artifactId> aspectjweaver </ artifactId>
    <version> 1.8.7 </ version>
    </ dependency>
    <dependency>
    <groupId> org.apache.struts </ groupId>
    <artifactId> struts2-core </ artifactId>
    <version> 2.3.24 </ version>
    < / dependency>
    <dependency>
    <groupId> org.apache.struts </ groupId>
    <artifactId> struts2-spring-plugin </ artifactId>
    <version> 2.3.24 </ version>
    </ dependency>
    <dependency>
    <groupId> mysql </ groupId>
    <artifactId> mysql-connector-java </ artifactId>
    <version> 5.1.6 </ version>
    </ dependency>
    <dependency>
    <groupId> jstl </ groupId>
    <artifactId> jstl </ artifactId>
    <version> 1.2 </ version>
    </ dependency>
    </ dependencies> [/ mw_shl_code]
    (三)SSH工程搭建
    1.建立包结构
    项目管理工具Maven3

  2. 添加弹簧配置文件
    拷贝资源下的配置文件文件夹中的applicationContext.xml中
    3.添加struts配置文件
    [mw_shl_code = AppleScript的,真]
    <?XML版本= “1.0”编码= “UTF-8”?>
    <!DOCTYPE支柱PUBLIC!
    “ - // Apache软件基金会// DTD struts配置2.3 // EN”
    “HTTP:/ /struts.apache.org/dtds/struts-2.3.dtd “>
    <支柱>
    <包名称=”默认”命名空间= “/”延伸= “支柱默认”>
    </包>
    </支杆> [/ mw_shl_code ]
  3. 修改web.xml 配置文件,添加以下配置
    [mw_shl_code = applescript,true] <listener>
    <listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class>
    </ listener>
    <context-param>
    <param-name> contextConfigLocation </ param-name >
    <param-value> classpath :applicationContext.xml </ param-value>
    </ context-param>
    <filter>
    <filter-name> openSessionInView </ filter-name>
    <filter-class> org.springframework.orm。 hibernate5.support.OpenSessionInViewFilter </ filter-class>
    </ filter>
    <filter-mapping>
    <filter-name> openSessionInView </ filter-name>
    <url-pattern> /
    </ url-pattern>
    </ filter-mapping >
    <滤波器>
    <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> [/ mw_shl_code]
    5.编写实体类
    [mw_shl_code = applescript,true]包cn.itcast.mavenweb.domain;
    / *
    部门
    • @作者管理员
    • /
      公共类Dep {

私人长期uuid;
私人字符串名称;
私人长途电话;
public long getUuid(){
return uuid;
}
public void setUuid(Long uuid){
this.uuid = uuid;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getTele(){
return tele;
}
public void setTele(String tele){
this.tele = tele;
}
} [/ mw_shl_code]
6.编写映射文件
在src / main / resources下创建包cn.itcat.mavenweb.domain

在此目录下创建映射文件dep.hbm.xml,内容如下:
[mw_shl_code = applescript,true] <?xml version =“1.0”encoding =“utf-8”?>
<!DOCTYPE hibernate-mapping PUBLIC“ - // Hibernate / Hibernate Mapping DTD 3.0 // EN”
“http:// hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“>
<hibernate-mapping>
<class name =”cn.itcast.mavenweb.domain.Dep“table =”dep“>
<id name =”uuid“>
< / id>
<property name =“name”/>
<property name =“tele”/>
</ class>
</ hibernate-mapping> [/ mw_shl_code]
7.创建数据访问层接口DepDao

package cn.itcast.mavenweb.dao;

import java.util.List;

import cn.itcast.mavenweb.domain.Dep;

/**
 * 部门Dao
 * @author Administrator
 *
 */
public interface DepDao {
public List<Dep> getList();
}
  1. 创建数据访问类DepDaoImpl
    [mw_shl_code = applescript,true]包cn.itcast.mavenweb.dao.impl;
    import java.util.List;
    import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
    import cn.itcast.mavenweb.dao.DepDao;
    import cn.itcast.mavenweb.domain.Dep;
    / *
    部门数据访问类
    • @author管理员
    • /
      public类DepDaoImpl扩展的HibernateDaoSupport实现DepDao {

@覆盖
公共名单<德普>的GetList(){
回报(名单<德普>)getHibernateTemplate()。找到(“从德普“);
}
} [/ mw_shl_code]
9.创建业务逻辑层接口
[mw_shl_code = applescript,true]包cn.itcast.mavenweb.service;

import java.util.List;

import cn.itcast.mavenweb.domain.Dep;

/ *
部门业务层接口

  • @作者管理员
  • /
    public interface DepService {

/ *
全部列表

  • @return
  • /
    public List <Dep> getList();
    }
    [/ mw_shl_code]
    10.创建业务逻辑类
    [mw_shl_code = applescript,true]包cn.itcast.mavenweb.service.impl;

import java.util.List;

import cn.itcast.mavenweb.dao.DepDao;
import cn.itcast.mavenweb.domain.Dep;
import cn.itcast.mavenweb.service.DepService;
/ *
部门业务逻辑类

  • @作者管理员
  • /
    公共类DepServiceImpl实现DepService {

私人DepDao depDao;
public void setDepDao(DepDao depDao){
this.depDao = depDao;
}

/ *
全部列表

  • /
    public List <Dep> getList(){
    // TODO自动生成的方法存根
    返回depDao.getList();
    }

} [/ mw_shl_code]
11.创建动作类
[mw_shl_code = applescript,true]包cn.itcast.mavenweb.web.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.mavenweb.domain.Dep;
import cn.itcast.mavenweb.service.DepService;

/ *
部门行动

  • @作者管理员
  • /
    公共类DepAction {

私人DepService depService;
public void setDepService(DepService depService){
this.depService = depService;
}

public String list(){
List <Dep> list = depService.getList();
ServletActionContext.getRequest()。setAttribute(“list”,list);
返回“列表”;
}
} [/ mw_shl_code]

  1. 编写弹簧配置文件的内容
    [mw_shl_code = applescript,true] <bean id =“depDao”class =“cn.itcast.mavenweb.dao.impl.DepDaoImpl”>
    <property name =“sessionFactory”ref =“sessionFactory”> </ property>
    </ bean >
    <bean id =“depService”class =“cn.itcast.mavenweb.service.impl.DepServiceImpl”>
    <property name =“depDao”ref =“depDao”> </ property>
    </ bean>
    <bean id =“ depAction“class =”cn.itcast.mavenweb.web.action.DepAction“>
    <property name =”depService“ref =”depService“> </ property>
    </ bean> [/ mw_shl_code]
    13.编写struts配置文件的内容
    [mw_shlcode = applescript,true] <action name =“dep *”class =“depAction”method =“{1}”>
    <result name =“list”> / index.jsp </ result>
    </ action> mw_shl_code]
    14.编写index.jsp的内容
    [mw_shl_code = applescript,true] <%@ page language =“java”contentType =“text / html; charset = UTF-8”
    pageEncoding =“UTF-8”%>
    <%@ taglib prefix =“c”uri =“ http://java.sun.com/jsp/jstl/core“%>;
    <!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 = UTF-8“>
    <title>测试页面</ title>
    </头>
    <BODY>
    开启神秘的Maven的世界〜结果
    <C:VAR的forEach = “i”的项= “$ {列表}”>
    $ {i.uuid} $ {i.name} $ {i.tele} <br>
    </ c:forEach>
    </ body>
    </ html> [/ mw_shl_code]
    (四)定义变量
    在pom.xml中中插入配置
    [mw_shl_code = applescript,true]
    <properties>
    <hinernate.version> 5.0.7.Final </hinernate.version>
    <spring.version> 4.2.4.RELEASE </spring.version>
    <struts.version> 2.3.24 </struts.version>
    </ properties> [/ mw_shl_code]
    此时相当于创建了三个变量。
    我们在下面可以通过EL表达式引用这三个变量的值
    [mw_shl_code = AppleScript的,真<依赖性>
    <groupId> javax.servlet </ groupId>
    <artifactId> servlet-api </ artifactId>
    <version> 2.5 </ version>
    <scope> provided </ scope>
    </ dependency>
    <dependency>
    <groupId> org.hibernate < / groupId>
    <artifactId> hibernate-core </ artifactId>
    <version> $ {hibernate.version} </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-core < / artifactId>
    <version> $ {spring.version} </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-beans </ artifactId>
    <version> $ {spring.version} </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-context </ artifactId>
    <version> $ {spring.version} < / version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-orm </ artifactId>
    <version> $ {spring.version} </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-test </ artifactId>
    <version>$ {spring.version} </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-context-support </ artifactId>
    <version> $ {spring.version} </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-aop </ artifactId>
    <version> $ {spring.version} </ version>
    </ dependency>
    <dependency>
    <groupId> org.springframework </ groupId>
    <artifactId> spring-web < artifactId>
    <version> $ {spring.version} </ version>
    </ dependency>
    <dependency>
    <groupId> org.aspectj </ groupId>
    <artifactId> aspectjweaver </ artifactId>
    <version> 1.8.7 </ version>
    </ dependency>
    <dependency>
    <groupId> org.apache.struts </ groupId>
    <artifactId> struts2-core </ artifactId>
    <version> $ {struts.version} </版本>
    </ dependency>
    <dependency>
    <groupId> org.apache.struts </ groupId>
    <artifactId> struts2-spring-plugin </ artifactId>
    <version> $ {struts.version} </ version>
    </ dependency>
    <dependency>
    <groupId> mysql </ groupId>
    <artifactId> mysql-connector-java </ artifactId>
    <version> 5.1.6 </ version>
    </ dependency>
    <dependency>
    <groupId> jstl </ groupId>
    <artifactId> jstl </ artifactId>
    <version> 1.2 </ version>
    </ dependency> [/ mw_shl_code]
    这样做的好处是一旦版本发生变化,直接修改属性标签里的值即可。
    (五)打的战争包
    执行命令package即可将战工程打成war工程。

项目管理工具Maven3

猜你喜欢

转载自blog.51cto.com/13587708/2120196
今日推荐