使用springBoot整合jsp,controller,mybatis都整合好了,但是在访问jsp页面的时候发现页面并没有进行渲染,而是直接返回了整个页面字符串。
jsp页面如下
controller如下
@Controller
public class UserCtrl {
@Autowired
AccountService AccountService;
@RequestMapping("/hello")
public String home (HttpServletRequest request){
Account account = AccountService.getAccountByName("Langdon");
System.out.println(account.getAccountName());
request.setAttribute("account",account);
return "/helloworld";
}
}
jsp对应的ViewResolver bean;
已确定该路径是生效的,如果注释下面代码会出现500异常.
/**
* JstlView
*/
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
访问 http://localhost:8080/hello 结果如下
从结果中可以看到页面并没有渲染
尝试过在properties直接写下面配置,但得到结果一样。尝试用别的浏览器访问,得到的结果也一样。
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
maven打包jar
<packaging>jar</packaging>
<build>
<finalName>ROOT</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
在filefox RESTClient请求
header如下
Status Code: 200
Accept-Ranges: bytes
Content-Language: zh-CN
Content-Length: 315
Date: Sat, 16 Jun 2018 08:51:57 GMT
ETag: W/"315-1529136546000"
Last-Modified: Sat, 16 Jun 2018 08:09:06 GMT
body如下
<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/include/taglib.jsp" %>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Helloworld</title>
</head>
<body>
<h1>hello ${account.accountName}</h1>
</body>
</html>
经过1天的奋战,看了很多springboot项目,终于找到原因;
maven配置问题;
错误配置如下:
正确配置为
附上maven.scope的用法;
maven.scope的用法