【发布时间】:2018-02-25 04:59:27
【问题描述】:
我正在学习使用 Spring Boot 和 Java 构建 Web 应用程序。当我通过 Spring Tool Suite 运行我的应用程序时,我的应用程序可以正常工作,但是在我使用 bootRepackage 构建可执行 jar 并运行它之后,它无法解析 freemarker 视图。
我不确定出了什么问题。任何帮助,将不胜感激。
以下是我的freemarker相关的application.properties,
spring.http.encoding.charset=UTF-8
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/,classpath:/templates/web/
我的 jar 结构,
BOOT-INF
classes
com
scss
static
templates
web
story.html
app
application.properties
log4j2.xml
META-INF
org
我的控制器,
@Controller
public class HomeController {
@Autowired
private AppLog appLogger;
@RequestMapping("/")
public ModelAndView Index(HttpServletRequest request) {
appLogger.log(Level.ERROR,AppLogSource.Web, "Reached Controller", null);
String testAttribute = request.getAttribute("com.demo.test").toString();
Map<String, String> vm = new HashMap<String, String>();
vm.put("testAttribute", testAttribute);
return new ModelAndView("/web/story", vm);
}
}
我确认我正在点击日志步骤,所以我认为问题在于解决视图,但我可能错了并且遗漏了其他东西。如果您需要更多信息,请告诉我。
再次感谢!
最好, 农历
编辑 Gradle 文件,
buildscript {
ext {
springBootVersion = '1.4.1.RELEASE'
}
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("com.moowork.gradle:gradle-node-plugin:1.2.0")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'com.moowork.node'
apply plugin: 'com.moowork.grunt'
jar {
baseName = 'testDemo'
version = '0.0.1'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
node {
version = '6.11.3'
npmVersion = '3.10.10'
download = true
}
task gruntMinifyJs(type: GruntTask){
args=['minifyJs', '--debug']
}
task gruntMinifyCss(type: GruntTask){
args=['minifyCss', '--debug']
}
task buildFrontEnd(type: GruntTask) {
args = ['default', '--debug']
}
npmInstall.dependsOn(nodeSetup)
buildFrontEnd.dependsOn(npmInstall)
gruntMinifyCss.dependsOn(npmInstall)
gruntMinifyJs.dependsOn(npmInstall)
build.dependsOn(buildFrontEnd)
configurations {
all*.exclude group: 'ch.qos.logback', module:'logback-classic'
all*.exclude group: 'ch.qos.logback', module:'logback-core'
}
dependencies {
compile('org.springframework.boot:spring-boot-devtools')
compile('org.springframework.boot:spring-boot-starter-freemarker')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter:1.4.1.RELEASE'){
exclude group:'org.springframework.boot', module:'spring-boot-starter-logging'
}
compile('org.springframework.boot:spring-boot-starter-jdbc'){
exclude group:'org.apache.tomcat', module:'tomcat-jdbc'
}
compile('mysql:mysql-connector-java')
compile('com.zaxxer:HikariCP-java6:2.3.13')
compile('org.springframework.boot:spring-boot-starter-log4j2:1.4.1.RELEASE')
compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.8')
compile('com.google.zxing:core:3.3.0')
compile('org.antlr:antlr4-runtime:4.5')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
编辑 3, 进一步更新,
所以我附加了远程调试器,我发现 spring 正在使用 ContentNegotiatingViewResolver 将视图解析为 InternalResourceView,但是当我通过 spring 工具套件执行时,它正确解析为 FreemarkerView。 我希望这可以帮助某人缩小我的问题范围。我会看看我是否可以通过调试器同时到达其他任何地方。
【问题讨论】:
-
您应该在此类问题中也包含错误消息(堆栈跟踪)。
-
不幸的是,我没有收到任何错误,所以不知道哪里出错了。运行打包的 jar 时是否有可以打开的调试或跟踪标志?
-
分解您的 jar 文件以确保 jar 文件位于 lib 目录中。
-
您说您没有收到错误消息(我假设您已经检查了日志和浏览器中返回的响应),但是您会得到什么,会发生什么?
-
@JCCarrilo 我今天回家后会试试看。
标签: spring spring-mvc spring-boot freemarker