【问题标题】:Deploying war file together with resources将war文件与资源一起部署
【发布时间】:2015-06-12 09:04:18
【问题描述】:

我有已部署的战争文件,但在部署资源和链接后无法正常工作。

<build>
    <finalName>KFA</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*Tests.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

当我构建应用程序时,我得到了部署到 tomcat 的 kfa.war 但不幸的是我无法访问任何页面,我得到 http 状态 404 - /kfa/实习生。样品控制器

 @Controller
@RequestMapping("/kfa/trainee")
 public class TraineeController {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
TraineeService traineeService;
RegService regService;

@Autowired
public void setTraineeService(TraineeService traineeService) {
    this.traineeService = traineeService;
}

@Autowired
public void setRegService(RegService regService) {
    this.regService = regService;
}


@RequestMapping(method = RequestMethod.GET)
public ModelAndView addMember(ModelMap model){
    model.addAttribute("module", "kfa/trainee");
    model.addAttribute("submodule", "add_trainee");
    model.addAttribute("title", "Trainee");
    model.addAttribute("subtitle", "Add");
    model.addAttribute("trainee", new Trainee());

    return new ModelAndView("/kfa/index", model);
  }
}

当我在 intellij idea 中运行和部署时,一切正常。我的问题是我应该如何配置上面粘贴的构建以允许我在另一台机器上的 tomcat 上运行 war 文件。

如果不明白,您可以询问更多信息。我会很高兴得到您的帮助。

【问题讨论】:

    标签: java maven spring-mvc tomcat intellij-idea


    【解决方案1】:

    如果您在 tomcat 容器中部署名为 kfa.war 的 war 文件,如果您在部署时未提供任何其他信息,则 tomcat 会假定上下文路径 /kfa。

    尝试访问/kfa/kfa/trainee。

    但是查看你的 tomcat 的日志文件应该会有所帮助!您可以安装 tomcat 的 manager war (https://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html) 以获取有关容器中已安装 WAR 的信息,转到 /manager/html。

    【讨论】:

    • 如果我不想复制 /kfa/kfa/trainee 怎么办?
    • 只需从@RequestMapping 中删除/kfa。我不知道你如何在 IntelliJ 中启动你的 webapp。如果您使用 maven 目标(jetty/tomcat)运行它,您可以在 pom.xml 中配置上下文路径。
    • 能否提供pom配置?这是我要部署的第一个应用程序...我正在使用 tomcat 运行它
    • 在此处查看名为“使用不同的上下文路径”的部分:tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/…
    • 太好了,@丹尼斯。也许您可以将答案标记为有用的答案?谢谢。
    【解决方案2】:

    通过解压缩验证您的战争是否包含所有应有的内容。

    如果它没有所需的内容,请确保您的项目使用正确的 maven 目录结构,如此处提到的 https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

    如果不是,您可能必须相应地重组您的项目,并且一切都应该按预期工作

    【讨论】:

    • 'src +++main +++++++java +++++++webapp ++++++++++++++资源 +++++ +++++++++++WEB-INF ' 在资源中,我拥有我的 css、图像和 js
    • 你的结构看起来不错。正如保罗所建议的那样,这似乎是您访问它的方式。您可能必须从映射和返回的模型中删除 /kfa 以使其与 /kfa/trainee url 一起使用
    • 如果我在 intellij idea 中从我的控制器中删除 /kfa 应用程序是使用 {host}:{port}/trainee 启动的,当我获取生成的 war 文件并部署在 tomcat 窗口上时,现在的访问权限是在 {host}:{port}/kfa/trainee 并返回错误。添加的kfa是由于提取的文件夹。我该如何配置以便创建文件夹但不考虑?
    • 在你的 tomcats conf/server.xml 你可以添加
    • 我试过了,但没有区别。请如果您可以使用我可以遵循的 intellij 想法编写示例应用程序,我将不胜感激。感谢@Johson Abraham 的帮助
    猜你喜欢
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2014-07-11
    • 2021-07-04
    相关资源
    最近更新 更多