可参考图文🔗:
https://www.jianshu.com/p/e6a764a4d311
1.设置指定项目的访问名称
resources->application.properties->
//设置访问路径
server.servlet.context-path=/springboot
//设置访问端口
server.port=....
再次访问时网址:
localhost:自定义的端口号/springboot(访问路径)/项目名…
2.整合jsp
(1)配置pom.xml文件
在<dependencies></dependencies>
中添加
<!-- servlet 依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL (JSP standard Tag Library) JSP 标准标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Tomcat的支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
⚠️注意:
如果出现报错:
服务器端日志输出为:
o.s.w.s.r.ResourceHttpRequestHandler : Path with “WEB-INF” or “META-INF”: [WEB-INF/views/home.jsp]
可尝试注释掉tomcat支持中
<scope>provided</scope>
我也不知道具体原因是什么注释掉之后就好了
(2)配置核心事务
resources->application.properties->
spring.mvc.view.prefix=/WEB_INF/pages
spring.mvc.view.suffix=.jsp
(3)创建/WEB_INF/pages文件夹
并在其中创建一个jsp页面,与方法名同名
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
hahahahhaha
静态跳转:
package com.chinasofti.springboot;
import com.chinasofti.springboot.vo.User;
import com.sun.org.apache.xalan.internal.xsltc.dom.SortingIterator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
//注意这里是静态controller,不跳转jsp页面
@RestController
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
//与SpringMVC一样设置访问 类似springMVC中的controller
@RequestMapping("hello")
public String hello() {
return "hello springboot";
}
@RequestMapping("hello2")
public String hello2(HttpServletRequest request) {
String account = request.getParameter("account");
String pwd=request.getParameter("pwd");
return account+pwd;
}
@RequestMapping("hello3")
public String hello3(String account,String pwd) {
return account+pwd;
}
@RequestMapping("hello4")
public String hello4(User user) {
return user.toString();
}
}
动态跳转
package com.chinasofti.springboot.Welcome;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@SpringBootApplication
public class StudentApplication {
@RequestMapping("home")
public String home() {
System.out.println("1111");
return "home";
}
}
依旧RUN SpringbootApplication()
访问
localhost:8080/springboot/home
可跳转到jsp页面内容。