java - Spring keeps returning string instead of jsp/html content -
i trying understand whole spring framework. knowledge, relatively new techniques of using gradle makes lot of tutorials , posts online outdated?
the main issue running in when try display jsp or html page, webpage's body shows text: "filename.jsp".
the project created new-> other-> spring starter project using gradle , sts 3.7. goal create web application using mvc pattern.
folder structure:
test
--spring elements (created sts)
--src/main/java
++--test
++++--testapplication.java (created sts)
++--test.controller
++++--jspcontroller.java
--sec/main/resources
++--application.properties (created sts , empty file)
--src/main/webapp ( ** created directory, required?)
++--web-inf
++++--home.jsp
++++--home.html
++++--web.xml (** additional question below)
testapplication.java
package test; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class testapplication { public static void main(string[] args) { springapplication.run(testapplication.class, args); } }
home.jsp
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>test</title> </head> <body> <h1>test 1</h1> <form> first name:<br> <input type="text" name="firstname"> <br> last name:<br> <input type="text" name="lastname"> </form> </body> </html>
jspcontroller.java
package test.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class jspcontroller { @requestmapping(value = "/jsp", method = requestmethod.get) public string home(){ return "/webapp/web-inf/home.jsp"; } }
web.xml
//empty file right
build.gradle
buildscript { ext { springbootversion = '1.2.5.release' } repositories { mavencentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}") classpath("io.spring.gradle:dependency-management-plugin:0.5.1.release") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'io.spring.dependency-management' jar { basename = 'test' version = '0.0.1-snapshot' } sourcecompatibility = 1.8 targetcompatibility = 1.8 repositories { mavencentral() } dependencies { compile("org.springframework.boot:spring-boot-starter-web") testcompile("org.springframework.boot:spring-boot-starter-test") } eclipse { classpath { containers.remove('org.eclipse.jdt.launching.jre_container') containers 'org.eclipse.jdt.launching.jre_container/org.eclipse.jdt.internal.debug.ui.launcher.standardvmtype/javase-1.8' } } task wrapper(type: wrapper) { gradleversion = '2.3' }
when go http://localhost:8080/jsp, html page has body: /webapp/web-inf/home.jsp
so jsp not displaying @ all. have tried html , or without method=requestmethod.get/post. nothing works.
**additional question: lot of online posts/tutorial goes in .xml files, example, web.xml. understanding, these no longer required or needed because spring + gradle generates .xml automatically @notations?
that's because using @restcontroller
instead of @controller
.
change class annotation
@restcontroller public class jspcontroller { ... }
to:
@controller public class jspcontroller { ... }
when annotate class restcontroller
, methods annotated @requestmapping
assume @responsebody
semantics default. in other words, method #home
serializing string /webapp/web-inf/home.jsp
json, instead of mapping it's value view.
Comments
Post a Comment