在windowns下的tomcat中部署web项目,配置,启动等详细步骤

一、遇到的问题

在idea中创建好的spring boot项目,可以完成前后台与数据库之间的交互。
打成war包,部署到tomcat的webapp里后,也自动解析了,也能访问 http://localhost:8080
好了这时候问题来了

1、问题一

当我访问http://localhost:8080/spring-1.0(spring-1.0是我war包的名字)的时候,出现404页面。

2、当我解决了第一个问题后,又出现第二个问题了。

当我访问http://localhost:8080/spring-1.0,自动进入了我的主页面index.jsp (为什么会自动进入这个页面?后面讲到),但是从这个页面与我的后台Controller进行交互的时候,又出现404了。
刚开始我以为是tomcat连接数据库,需要在tomcat的配置文件里,配置数据源啥的,饶了一大圈,发现自己错了(原谅我是一只菜鸟)。

二、解决它

分析问题一

问题一出现404,后面我看了tomcat的配置文件,我感觉是因为/conf/web.xml里面的默认配置有关。
查找资料:

<welcome-file-list>标签下可以设置多个首页<welcome-file>,容器启动后会在根目录下依次查找匹配的物理存在的文件,返回第一个找到的文件,没有找到报404错误。

tomcat的默认配置如下,
在这里插入图片描述
我的项目:如图
在这里插入图片描述
我的解决方式是,直接在webapp下建立index.jsp页面,然后重写打包部署,就行了。再次访问http://localhost:8080/spring-1.0,就不会404了,会自动进入到index.jsp页面
其实这里也可以更改tomcat目录下/conf/web.xml里面的配置:
在这里插入图片描述
比如指定web-info/limian.jsp为首页,那么在启动后,访问http://localhost:8080/spring-1.0就会进入limian.jsp页面
在这里插入图片描述

分析问题二

资料:

  • jar包启动步骤:执行SpringBoot主类的main方法,启动ioc容器,创建嵌入式的Servlet容器;
  • war包启动步骤:启动服务器,服务器启动SpringBoot应用【SpringBootServletInitializer】,启动ioc容器;

从上知道,原来是我的启动类没有 extends SpringBootServletInitializer
因为我是打成war包的,启动tomcat服务器后,它会去启动SpringBootServletInitializer抽象类,但是我的启动类里没有继承这个,所以尝试访问时,就访问不了,就404了
继承之后,请求才能访问到controller
在这里插入图片描述
control

package com.ruolan.action;

import com.ruolan.entity.UserEntity;
import com.ruolan.entity.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class WebAppPageAction {

    @Autowired
    private UserRepository userRepo;

    //到index页面
    @RequestMapping("/go")
    public ModelAndView backWebappPage() {
        return new ModelAndView("index");
    }

    @RequestMapping("/login")
    public String login(UserEntity user) {
//        Sort sort = new Sort(Sort.Direction.ASC,"字段名");//排序
        //根据值来筛选数据
//        UserEntity list = userRepo.findUserByActAndPwd(user.getLoginAccount(),user.getPassword());
        List<UserEntity> list = userRepo.findAll();
        boolean temp = false;
        for (UserEntity user1 : list) {
            if (user1.getLoginAccount().equals(user.getLoginAccount())
                    && user1.getPassword().equals(user.getPassword())) {
                temp = true;
            }
        }
//        new ModelAndView("index");
        if (temp) {

            return "success";
        } else {
            return "error1";
        }
    }
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        return "服务部署没问题";
    }

    @RequestMapping("/db")
    @ResponseBody
    public List<UserEntity> db() {
        return userRepo.findAll();
    }
}

index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>hello,index.jsp</h1>
<br>
welcome,yl,success
<h1>this a home page</h1>

<a href="user/login.jsp">登陆</a>
</body>
</html>

login.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div>
    <%--
    <%=request.getContextPath()%> =
    http://localhost:8080/spring-1.0
    --%>
    <form action="<%=request.getContextPath()%>/login" method="post">
        <h3>登陆</h3>
        <div>
            <span>账号:</span><input type="text" placeholder="输入账号" name="loginAccount">
        </div>
        <div>
            <span>密码:</span><input type="password" placeholder="输入密码" name="password">
        </div>
        <div>
            <input type="submit" value="提交">
        </div>
    </form>

</div>
</body>
</html>

error1.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登陆失败</h1>
jsp页面
</body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
</html>
<body>
<h1>登陆成功</h1>
这是jsp页面
</body>

最后

打包部署,再访问
http://localhost:8080/spring-1.0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值