依赖管理

Jar包的管理

需求:整合struts2   页面上传一个客户id 跳转页面

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

添加依赖:

打开maven仓库的视图:

 maven的依赖管理详细 随笔 第1张

重建索引

 maven的依赖管理详细 随笔 第2张

1、 创建maven项目(同上)

2、 跳过骨架(同上)

3、 填写坐标信息(同上)

4、 添加web.xml文件(同上)

5、 修改编译版本(同上)

6、 添加坐标 选择Dependencies标签 点击add

 maven的依赖管理详细 随笔 第3张

7、 手动输入要添加的坐标,选择版本

 maven的依赖管理详细 随笔 第4张

8、 可以看到 pom.xml文件中多出了如下代码

 maven的依赖管理详细 随笔 第5张

9、 同样的方式添加servlet-api.jar和jsp-api.jar 注意选择scope为provided

 

10、 写action代码

maven的依赖管理详细 随笔 第6张
import com.opensymphony.xwork2.ActionSupport;

public class CutomerAction extends ActionSupport {
  private Long custId;
  public Long getCustId() {
    return custId;
}

public void setCustId(Long custId) {
  this.custId = custId;
}
public String findById(){
  return SUCCESS;
}
}
maven的依赖管理详细 随笔 第7张

 

11、 添加struts.xml文件放到resources目录中

 maven的依赖管理详细 随笔 第8张

内容:

maven的依赖管理详细 随笔 第9张
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 配置常量 -->
<!-- 字符集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>

<!-- 开发模式 -->
<constant name="struts.devMode" value="true"></constant>

<!-- 通用package -->
<package name="customer" namespace="/" extends="struts-default">
<action name="find" class="cn.itcast.action.CutomerAction"
method="findById">
<result name="success">/jsp/info.jsp</result>
</action>
</package> </struts>
maven的依赖管理详细 随笔 第10张

12、 添加jsp页面

 maven的依赖管理详细 随笔 第11张

15、修改web.xml文件 添加过滤器

maven的依赖管理详细 随笔 第12张
<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>
maven的依赖管理详细 随笔 第13张

依赖范围

3.1 Compile  struts2-core

编译(compile)时需要 测试时需要,,运行时需要,打包时需要

3.2 Provided  jsp-api.jar   servlet-api.jar

编译(compile)时需要,测试(test)时也需要 ,运行时不需要,打包时不需要

3.3 Runtime   数据库驱动包

编译时不需要,测试时需要,,运行时需要,打包时需要

3.4 Test  junit.jar

编译时不需要,测试时需要,运行时不需要,打包也不需要

 maven的依赖管理详细 随笔 第14张

添加插件

  Maven  add  plugin

如果用tomcat7运行用命令:

  Tomcat7:run

常见问题:

 maven的依赖管理详细 随笔 第15张

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄