Springboot学习——文件配置&整合jsp

可参考图文🔗:
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页面内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值