一、遇到的问题
在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