UPDATE: Switched the annotation from #RestController to #Controller and now I simply get a 404 when trying to hit http://localhost:8080/api/v1/create_short_url. I added a System.out.println into the controller and see it being printed, so I know it's making it into the controller. I think it just can't find the template.
#Controller
#RequestMapping("/api/v1")
public class UrlShorteningController {
#GetMapping("/create_short_url")
public String newShortUrl(Model model) {
System.out.println("^^^^^^^");
model.addAttribute("longUrl",
new String());
return "new-short-url-form";
}
Request
I have the a controller which I expect to render an HTML template. Instead, it just returns the name of the controller. What am I doing wrong here?
Actual:
Expected:
Rendering of the html page
Code
Controller in src/main/java/com/example/urlshortener/api/UrlShorteningController.java:
#RestController
....
#GetMapping("/create_short_url")
public String newShortUrl(Model model) {
model.addAttribute("longUrl",
new String());
return "new-short-url-form";
}
build.gradle:
...
dependencies {
...
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
...
}
Thymeleaf Template in src/main/resources/templates/new-short-url-form.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>New Short Url</title>
</head>
<body>
<form method="POST" th:action="#{/create_short_url_thymeleaf}" th:object="${String}">
<h1>Enter a url to shorten</h1>
<input type="text" id="longUrl" th:field="*{String}"/>
</form>
</body>
</html>
Thymeleaf Template in src/resources/templates/new-short-url-form.html
The path for templates should be src/main/resources/templates/
You're using a GetMapping instead of a RequestMapping. Use RequestMapping if you expect to provide a template. GetMapping is only for a get request which explains why you're receiving the literal String and not the template.
I realized my editor wasn't picking up the view name. I have no idea, why, as the folder structure seemed correct. I tried renaming it to "some-form" and it suddenly works. Not sure if there is a length restriction on html file names top be picked up.
Try using ModelAndView
#Controller
#RequestMapping("/api/v1")
public class UrlShorteningController {
#GetMapping("/create_short_url")
public ModelAndView newShortUrl() {
ModelAndView modelAndView = new ModelAndView();
System.out.println("^^^^^^^");
modelAndView.addObject("longUrl",
new String());
modelAndView.setViewName("new-short-url-form")
return modelAndView;
}
And replace your html tag with <html lang="en" xmlns:th="http://www.thymeleaf.org">
Related
This is my pom.xml file
pom.xml enter image description here
Application.properties File
#spring.thymeleaf.enabled=true
#spring.thymeleaf.prefix=/templates/
#spring.thymeleaf.suffix=.jsp
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.jsp
Home.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Registration</h1>
</body>
</html>
Controller
#RestController
public class LoginController {
#GetMapping(path="/home")
public String loginPage() {
System.out.println("hello");
return "home";
}
}
Application File
#SpringBootApplication
public class SpringJspDemoApplication {
public static void main(String[] args)
{
SpringApplication.run(SpringJspDemoApplication.class, args);
}
}
Output:
enter image description here this is the output I am getting a string value instead of jsp
Tried using Thymeleaf, tried to change #RestController-> #Controller, used #ComponentScan etc but issue not getting resolved.
Tried going through the solutions given on site but none of them are working for me, also they are quite older.
Project Structure :
enter image description here tried to put home.jsp at /webapp/WEB-INF/jsp/ that is also not working.
what will be the possible solution on this, can anyone please guide on this?
Below is the code when I change #Controller to #RestController
[![
Controller File
Blockquote
]1]1
And result is still
I have checked the status under network tab which is 200 only
Please Add #ComponentScan(basePackages={"com.web"}) in your main application file
Replace #RestController to #Controller like below
#Controller
public class LoginController {
#GetMapping(path="/home")
public String loginPage() {
System.out.println("hello");
return "home";
}
}
I'm making a simple SpringBoot app that returns a word inserted by the user via a form. I managed to get the response in another view (the form is in home.html, the answer in answer.html) but now I want to get the response in the "home" view. I tried to adapt the code from the two-view version, but it doesn't work. Any thoughts? Thanks!
the HTML view:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add a word</title>
</head>
<body>
<th:form action="/" object="word" method="POST">
<label>Dictionary - Add a word</label>
<br>
<input name="value">
<br>
<button type="submit">Submit</button>
</th:form>
<p th:text="${value}"></p>
</body>
</html>
The controller:
#RestController
public class Controller {
#GetMapping("/")
public ModelAndView home() {
return new ModelAndView("home", "word", new Word());
}
#PostMapping("/")
public ModelAndView home(#ModelAttribute("word") Word w, Model model) {
ModelAndView mAv = new ModelAndView("home", "word", model);
model.addAttribute("value", w.getValue());
return mAv;
}
}
Let's consider this request handler:
#GetMapping("/")
public Mono<String> index(Model model) {
model.addAttribute("list", Flux.just("item1", "item2"));
return Mono.just("list"); // <- Template name
}
If there is an index.html template in the templates directory, this handler is not excuted.
If I return Mono.just("index") from the handler:
#GetMapping("/")
public Mono<String> index(Model model) {
model.addAttribute("list", Flux.just("item1", "item2"));
return Mono.just("index"); // <- Template name
}
This handler is still not used.
It seems like I have to remove the index.html template from the templates directory for the GetMapping("/") route to be handled.
So my question is, is it possible to return a Mono.just("index") from a GetMapping("/") handler ?
As per this book, you should use #Controller instead of #RestController.
And here is another sample:
#GetMapping("/welcome")
public Mono<String> hello(final Model model) {
model.addAttribute("name", "Foo");
model.addAttribute("city", "Bar");
String path = "hello";
return Mono.create(monoSink -> monoSink.success(path));
}
The corresponding thymeleaf html page:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<title>Greet</title>
</head>
<body>
<p th:text="${name}"></p> lives in <p th:text="${city}"></p>
</body>
</html>
I am trying to build an application with spring boot and java in eclipse.
What I have tried so far:
HomeController.class
#Controller
#ComponentScan(basePackages = {"project.tool.frontend"})
public class HomeController {
#GetMapping("/home")
public String home(){
System.out.println("open page");
return "home";
}
}
home.html
<!DOCTYPE HTML>
<html>
<head>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="/home" method="get">
First name: <input type="text" name="fname"><br> Last
name: <input type="text" name="lname"><br> <input
type="submit" value="Submit">
</form>
</body>
</html>
Application.class
#SpringBootApplication
public class Application{
public static void main(String agrs[])
{
SpringApplication.run(Application.class);
}
}
When I run this Application I get the "Whitelabel ErrorPage" Error. I also enabled the whitelabel errorpage in my application.properties .
And a question for unserstandig: When I use #GetMapping("/home") in my HomeController and the <form action="/home" in my html file it will be mapped through the "/home" get method right?
Thank you all.
As a static resource, your home.html should be at resources/public/home.html (or /static/). Is it there?
Your Application should tell in which package your controller is located, so it gets picked up
Does adding a ComponentScan help?
#SpringBootApplication
#ComponentScan(basePackages = { "com.acme.controllers" })
public class Application{
}
If you have not configured any view resolver, try returning "/views/home.html" instead of just "home". Change "/views/home.html" to whatever be the path of the file.
I suggest using Thymeleaf as your viewresolver.
You can place the home.html in src/main/resources folder to be recognized.
As you are using .html extension then I suggest you to use the thymeleaf as a view resolver.
Using thymeleaf HTML:
<form th:action="#{/home}" th:method="get"
...
...
//Rest of your code goes here.
</form>
Use #ComponentScan annotation:
#SpringBootApplication
#ComponentScan(basePackages = { "<full package name where controller exists>" })
public class Application{
public static void main(String agrs[]){
SpringApplication.run(Application.class);
}
}
I am sending Object with help of ModdelAndView in Spring controller, but i am not able to read it on jsp?
JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
Tried with EL:
${user}
Try with JSTL:
<c:out value="${user}"></c:out>
</body>
</html>
controller:
#Controller
public class StudentHome {
#RequestMapping(value = "/auth/Home")
public ModelAndView RedirectLogin() {
ModelAndView modelAndView = new ModelAndView("/auth/Home");
modelAndView.addObject("user", "Alex");
return modelAndView;
}
}
I have tried both Spring EL and jstl its not working. Do i need to include anything else?
We need to include org.springframework.web.servlet.ModelAndView instead of org.springframework.web.portlet.ModelAndView;
The model presents a placeholder to hold the information you want to display on the view. It could be a string, which is in your above example, or it could be an object containing bunch of properties.
update your code as follows
#Controller
public class StudentHome {
#RequestMapping(value = "/auth/Home")
public ModelAndView RedirectLogin() {
return new ModelAndView("yourJspName","user", "Alex");
}
}
then in your jsp, to display the message, you will do
Hello ${user}!
hope this will help you..!