在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里面的默认配置有关。
查找资料:

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

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

分析问题二

资料:

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

从上知道,原来是我的启动类没有 extends SpringBootServletInitializer
因为我是打成war包的,启动tomcat服务器后,它会去启动SpringBootServletInitializer抽象类,但是我的启动类里没有继承这个,所以尝试访问时,就访问不了,就404了
继承之后,请求才能访问到controller
在windowns下的tomcat中部署web项目,配置,启动等详细步骤_第5张图片
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 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 db() {
        return userRepo.findAll();
    }
}

index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


hello,index.jsp


welcome,yl,success

this a home page

登陆

login.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


<%-- <%=request.getContextPath()%> = http://localhost:8080/spring-1.0 --%>

登陆

账号:
密码:

error1.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


登陆失败

jsp页面

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title



登陆成功

这是jsp页面

最后

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

你可能感兴趣的:(tomcat,发包部署)