springBoot 学习记录(四)- 访问jsp页面

来源:互联网 发布:零基础java难学吗 编辑:程序博客网 时间:2024/04/29 20:43

最近在弄springBoot 项目的时候写的jsp页面,直接访问jsp的时候是jsp源码,解决办法如下:


1;在pom.xml  添加依赖:

<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>


pom.xml 代码:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.phpfzh2</groupId>
<artifactId>spring-boot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.20</version>
</dependency>


<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.25.4-RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
 
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


2 :在webapp 新建 test.jsp文件:





test.jsp 代码:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <a href="http://localhost:8080/download1">下載png文件</a>
  <a href="http://localhost:8080/download2">下載tar1文件</a>
 <a href="http://images.chenjiaming.com/group1/M00/00/00/rBKphlnDFduAf-RkAFLMV62e0mQ027.rar">下載tar文件</a>
 </body>
</html>


启动类Application代码:

package com.phpfzh;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {


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



运行项目:


右击项目 点击 run As   ==> Maven clear    ====> maven install  

 右击项目 点击 run As ===> java application ===》 在浏览器输入:localhost:8080/test.jsp

测试完成,这样直接访问jsp 就不会显示jsp源码了。





3 配置SpringMVC 

 

在application.yml配置文件添加:

spring: 

 mvc:  
    view:  
        prefix: /WEB-INF/view/  
        suffix: .jsp



application.yml 代码:

spring:
  mvc:
    view:
      prefix: /WEB-INF/view/
      suffix: .jsp
fdfs:
  soTimeout: 1501
  connectTimeout: 601 
  thumbImage:             #缩略图生成参数
    width: 150
    height: 150
  trackerList:            #TrackerList参数,支持多个
    - 192.168.133.128:22122


注意:yml 文件格式。不能用tab键缩进(会报错),空格按两下就可以了。


在WEB-INF新建view文件夹,在view 文件夹下新建index.jsp文件





index.jsp 代码:


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index</title>
</head>
<body>
Index 测试jsp
</body>
</html>



新建TestController 类测试,代码如


package com.phpfzh.web;


import java.math.BigDecimal;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.phpfzh.mode.User;


@Controller
public class TestController2 {


@ResponseBody
@RequestMapping("/test2")
public User test2(){
User user = new User();
user.setId(new BigDecimal(12));
user.setSex("12");
user.setUsername("phpfzh-test2");
return user;
}

  @RequestMapping("/index")
public String index(){
  return "index";
}
}




运行项目:


右击项目 点击 run As   ==> Maven clear    ====> maven install  

 右击项目 点击 run As ===> java application ===》 在浏览器输入:localhost:8080/index





原创粉丝点击