spring boot tomcat jsp

2022年01月15日 阅读数:0
这篇文章主要向大家介绍spring boot tomcat jsp,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

spring boot 1.3.6.RELEASEjava

 

1、 spring boot tomcat jspweb

1.   maven webapp为例spring

pom.xml所需配置apache

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

spring mvc 配置tomcat

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

启动类mvc

@SpringBootApplication
public class App extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(App.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}

controllerapp

@Controller
@RequestMapping("user")
public class UserController {
    @RequestMapping
    public String index(Model model) {
        model.addAttribute("s", "s");
        return "user";
    }
}

三种启动方式:webapp

1)   找到target目录下打包好的war文件,使用下面命令运行jsp

java –jar xx.warmaven

2)   使用spring-boot-maven-plugin插件运行

a)   在pom.xml所在目录以命令方式运行:

mvn spring-boot:run

b)   使用idea时:

3)   将项目放到tomcat中运行

启动方法必需继承SpringBootServletInitializer

 

*若是el表达式没有生效

1.在jsp页面加上

     <%@ page isELIgnored="false"%>

2.查看项目是否包含web.xml

     若是是2.3,下图所示:

   

    默认是开启了忽略el表达式的,换为更高版本的便可