How to create the file browser in view for spring roo? - spring

I'm new in spring roo. I want to create a page with file upload. I used spring roo to create all pages and i try to use it to create a file browser button in file upload page. The problem is spring roo using spring form tag which doesn't have file browser. I solve this problem by using html input type="file" tag instead, but the spring roo showed the error like this "Failed to invoke handler method [public void egat.spring.roo.ptu.io.web.ImportExcelController.post(java.lang.Long,org.springframework.ui.ModelMap,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]; nested exception is java.lang.IllegalStateException: Could not find #PathVariable [id] in #RequestMapping "
How can i solve this problem?

There is defect in jira raised against this issue you can vote for it.
https://jira.springsource.org/browse/ROO-442

Spring file upload in a mixed form
Check this out till this bug is resolved

Related

Unable to locate element using HtmlUnit and WebDriver on Spring Boot

Same title question already exists, however the environment seems to be different.
I'm trying to execute Spring Boot Web MVC test using MockMvc and WebDriver, so I don't need to run Selenium Server.
I created sample project on GitHub as public repository.
I try to impelent the code referencing Spring Framework Document and Spring Boot Document.
The test code works when using MockMvc and HtmlUnit (MessageControllerMockMvcAndHtmlUnitTest.java).
However, when I execute the test using MockMvc and WebDriver (MessageControllerMockMvcAndWebDriverTest.java), the error occurs as follows:
java.lang.IllegalStateException: Unable to locate element by name for com.gargoylesoftware.htmlunit.TextPage#~
Does anyone know what is wrong and how to fix it?
I finally solved the problem by myself.
I just made 2 mistakes.
I misstyped the URL (messages not messagges)
I tried to operate hidden element. So I need to operate by JavaScript.
before (wrong)
this.id.sendKeys(id);
after (correct)
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("document.getElementById('id').value = '" + id + "';");

passing a Spring Exception to a Thymeleaf page

Having added the error.html Thymeleaf template to replace the default error page on Spring how can I get access to the exception on that page?
So I have custom exception thrown on some controller. How to access that exception on the Thymeleaf page?
I want this to work globally, so any controller thrown exception should be accessible.
Case 1: For customized error pages with spring default error info
There are some predefined objects in Thymeleaf to show error information, e.g. ${error}, ${exception} and so on. These objects can be used in your customized error page.
This article will help you more. Custom Error Page with Thymeleaf
Case 2: For customized error pages with customized error info
There are #ControllerAdvice and #ExceptionHandler used to handle exceptions in controllers globally. A certain exception can be add to Modal's attribute and therefore accessed in thymeleaf templates. However, please be noted that the solution does not work in Webflux.
The blog shows details about the solution. Exception Handling in Spring MVC

What is exactly server.error.path property?

In Spring Boot, what is the purpose of server.error.path property in application.properties file?
The documentation just says:
Path of the error controller
But I want a clear description of this property with an example.
server.error.path - used as part of url for error pages.
site.getBaseUrl() + "/error"
For example some error happen on server side and you decide redirect user to error page like this:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/images/custom-error-page-aws-404-example.png
Code example of error controller you can find here:
https://www.logicbig.com/tutorials/spring-framework/spring-boot/implementing-error-controller.html
You can use this property in #RequestMapping("/error"). But instead of "/error" you can use "${server.error.path}"
UPDATE:
Also, Spring Boot BasicErrorController use server.error.path property
Property server.error.path in spring boot application used to define an error path while dealing with custom error handler. In Spring we create custom error handler using functional interface ErrorController, ths interface has a String type method getErrorPath which helps us to return the error page path(our error page as view).
But from Spring 2.3.0 this getErrorPath() method has been deprecated and replaced with server.error.path to manage the error path.
e.g. server.error.path=/error
For more detail about interface ErrorController, please refer Spring doc for ErrorController

Using Vaadin Error View instead of Spring Boot's Whitelabel error page

I'm using Spring Boot with Vaadin and. By using the #Autowired SpringNavigator, I have also set the error view:
navigator.setErrorView(ErrorView.class);
But when I type wrong URL like http://localhost:8080/asdf .
I always get the Spring Boot's Whitelabel error page. I know that I can set custom HTML page for the error like
/resources/public/error/404.html
but this is HTML way, I would like to use Vaadin with components,
the best solution would be the mentioned error view.

How to use Spring Hatoas ControllerLinkBuilder for a Thymeleaf templated scheduled email

I'm using Spring Hateoas in a Boot app to avoid manual creation of links in the view. It works great in Thymeleaf views, it works when a controller calls a service to send an email that is also rendered by Thymeleaf.
The code to create link is pretty standard
this.readLink = linkTo(methodOn(PostController.class)
.readPost(eventId, postId))
.withRel("ReadPost");
But for a #Scheduled service generated email, it fails like this
015-08-23 22:28:40.886 ERROR 1180 --- [pool-2-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.
java.lang.IllegalStateException: Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?
at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.hateoas.mvc.ControllerLinkBuilder.getCurrentRequest(ControllerLinkBuilder.java:242) ~[spring-hateoas-0.18.0.RELEASE.jar:na]
at org.springframework.hateoas.mvc.ControllerLinkBuilder.getBuilder(ControllerLinkBuilder.java:189) ~[spring-hateoas-0.18.0.RELEASE.jar:na]
at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:121) ~[spring-hateoas-0.18.0.RELEASE.jar:na]
Is there anything I can do to get around the lack of an HttpServletRequest due to the code running as a #Scheduled job?
ControllerLinkBuilder currently can only be used from within a request as only that allows it to create a fully qualified link using server and port information from it.
Within an #Scheduled-invoked method that information is not available. If you provide more information on what you're actually creating in that very method, I can suggest workarounds.

Resources