How to avoid Spring Boot Controller catching request for favicon? - spring

I have the following Controller
#Controller
public class MyController {
#GetMapping({"/", "/{name}"})
public String hello(#PathVariable(required = false) String name) {
System.out.println("Name: " + name);
return "hello";
}
}
and this directory structure
- resources
- static
- favicon.ico
- templates
- hello.html
When I GET http://localhost:8080/ through the browser, my application prints:
Name: null
Name: favicon.ico
So the request for /favicon.ico goes was caught by my controller, but instead I want that Spring handles this request and returns the favicon.ico placed here resources/static/favicon.ico
I think there should be a way without adding a handful of lines of configuration.

One approach you could take is to disable favicon resolution via this property:
spring.mvc.favicon.enabled=false

I had to include <link rel="icon" type="image/x-icon" href="images/favicon.ico"> in my html head, and it worked. The images prefix isnt necessary, but I moved the icon into their because I was allowing any unauthenticated request to /images so I dont need a extra rule for the favicon.ico. Thats my current directory strucutre:
- resources
- static
- images
- favicon.ico
- templates
- hello.html
s/o to Ajay Kumar for helping.

Related

Spring mapping resources for template Thymeleaf

Spring has problem with mapping items like css, js, img. Problem occured after when I clicked link from template index:
#RequestMapping("/")
public String index(Model model) {
return components.init("index",model).getHeader().getFooter().getSidebar().getRecommended().toString();
}
Link inside template the index looks like that: /places/tags(parameters)
#RequestMapping("/places/tags")
public String index(Model model,#RequestParam(required = false, defaultValue = "eats", value="listOfTag") String listOfTag) {
System.out.println(listOfTag);
return components.init("places",model).getHeader().getFooter().getSidebar().getPlaceForSidebar(listOfTag).toString();
}
After I clicked above link the places site looks not good.
Problem is with the mapping.
No mapping found for HTTP request with URI [/places/assets/css/styles.min.css]
I tried to register resources but nothing to change. I thinking that problem is on the site configuration of Thymeleaf.
I found solution: add before link "/" Topic should be close:)

Spring HTML URL View Not Found

I have a simple question. In a Spring Boot Application I have a controller that works fine:
#GetMapping("/mycats")
public String getCats(){
return "cats.html";
}
cats.html is a html file in resources/static/
When I change the action URL like this
#GetMapping("/mycats/my")
public String getCats(){
return "cats.html";
}
Spring cannot find the html file anymore. I have tried many directory combinations and still no success. I don't use thymeleaf/jsp. Why is that happening?
This is due to the context. When you use "mycats" it will look for the page in static directory. but when you use "mycats/my" it will look for the page in the static/my directory. This directory does not exists, so you get a 404 error.
You can make a little change to you controller. You can command it that look for in the previos directory with "../", but you always have to be only on directory deep.
#GetMapping("/mycats/my")
public String getCats(){
return "../cats.html";
}
Or from any directory, you can tell spring that looks at root directory with "/"
#GetMapping("/mycats/my")
public String getCats(){
return "/cats.html";
}

Redirect a website url to another website url - GoDaddy

I have a domain let say xyz.com, and I have an internal URL like this, xyz.com/testUrl. I need to redirect this URL (xyz.com/testUrl) to another web site URL.(which is URL like this ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com:3001/employee).
Now, whenever someone hits xyz.com/testUrl , he should be redirected to ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com:3001/employee.
I am using goDadddy hosting.
Just create .htaccess file in web root of xyz.com with content:
Redirect 302 /testUrl xxx.compute-1.amazonaws.com:3001/employee
This will do the required work.
#RequestMapping(value = "/xyz.com/testUrl", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com:3001/employee");
}

Liferay 6.1 + Spring MVC Portlet 4.0: Friendly URL Mapping

I'm trying to get friendly URL mapping to work for a Spring MVC portlet inside Liferay 6.1 and fail.
My additions to the liferay-portlet-xml are according to the manual and blog examples available and as follows:
<friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class>
<friendly-url-mapping>search</friendly-url-mapping>
<friendly-url-routes>com/.../friendly-url-routes.xml</friendly-url-routes>
with the friendly-url-routes.xml being
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.1.0//EN"
"http://www.liferay.com/dtd/liferay-friendly-url-routes_6_1_0.dtd">
<routes>
<route>
<pattern>/{match}</pattern>
<generated-parameter name="foo">{match}</generated-parameter>
</route>
</routes>
My Spring MVC controller goes like
#Controller
#RequestMapping("VIEW")
public class CarModelController {
#ActionMapping
public void action(#RequestParam("foo") final String testParam,
final ActionRequest request, final ActionResponse response) {
this.logger.info("default action");
this.logger.info("testParam = {}", testParam);
}
#RenderMapping
public String render(final RenderRequest request, final RenderResponse response) {
this.logger.info("default render");
return "index";
}
}
If I call my portlet using /baseurl/-/search/bar only the render phase output occurs, the action method isn't called.
If I create a link to this page using
<portlet:actionURL var="lastStepUrl">
<portlet:param name="foo" value="bar" />
</portlet:actionURL>
the URL Liferay generates looks like /baseurl/-/search/bar?p_auth=sometoken&p_p_lifecycle=1. It executes the action phase correctly and I'm also able to call that URL directly. It does, however, include the p_auth and p_p_lifecycle parameters that I want to get rid of.
Any suggestions are welcomed warmly.
I don't know much about Friendly URL in Liferay.
But I believe you can't completely remove p_p_lifecycle from url because.
this parameter tells Liferay which action to perform. This paramater has two values (0 or 1).
0 tells Liferay to just render the portlet,
whereas 1 tells Liferay to call the process Action Method.
Lets say you want to remove from certain action URL then can do it like this
<pattern>"your URL pattern"</pattern>
<implicit-parameter name="p_p_lifecycle">1</implicit-parameter>
<implicit-parameter name="javax.portlet.action">"Your action"</implicit-parameter>
As we know 1 for action phase we can hard code and put it into routes.xml file.same way for any render URL we can put 0
To remove p_auth try putting below properties in portal-ext.properties file
portlet.add.default.resource.check.enabled=false
auth.token.check.enabled=false

Laravel views do not output anything

I'm new to PHP Laravel framework. I'm studying it and playing with simple examples of code. My problem is that my views do not output anything - a blank white screen appears when I try to reach controller methods, for example, localhost/my-application/cms/action1
My routes file:
Route::controller('cms', 'CmsController');
My controller:
class CmsController extends BaseController {
public function getIndex()
{
View::make('cms.index');
}
public function getAction1()
{
View::make('cms.action1');
}
public function getAction2()
{
View::make('cms.action2');
}
}
My views are located in views/cms. They are very simple, for example:
<h1>Action1</h1>
<?php echo 'this is Action1'; ?>
And these views do not output anything, just simple blank white page appears. I tried to:
1) rename views files, and Laravel threw exception - "view not found", or so.
2) move view::make() methods to Routes file - the views were displayed then.
So where is the problem?
The bootstrap index.php file in laravel is inside the public folder.
So unless you've created a vhost for your application, you have to access it like
localhost/my-application/public/cms/action1
EDIT
Forget it. The problem is that you do not return the view::make from each function.
return View::make('cms.index');

Resources