Spring HTML URL View Not Found - spring

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";
}

Related

srpingboot routing problem with thymeleaf

I'm sorry I'm not very specific with the problem this is because I don't know exactly how to discribe the issue, I'm new to sringboot. what I basically want to do is to create a link like http://localhost/admin/countries and http://localhost/admin/countries/add
#Controller
public class CountryController {
#GetMapping("/admin/countries")
public String getCountries() {
return "admin/views/country/index";
}
//add new record
#GetMapping("/admin/countries/add")
public String addCountry() {
return "admin/views/country/add";
}
}
The Link loads correctly without problem, the second link loads but the styling of the page didn't load. I'm using thymeleaf templating so the two file shares the same layout. the index file loads correctly while the add file loads without style
I try to re-route the second link and have something like below and it serve the fike correctly with the style
#Controller
public class CountryController {
//add new record
#GetMapping("/admin/add-country")
public String addCountry() {
return "admin/views/country/add";
}
}
Now I'd like to have something like http://localhost/admin/countries/add, http://localhost/admin/countries/view, http://localhost/admin/countries/update rather than http://localhost/admin/add-country, http://localhost/admin/view-country
What is the cause of this problem and how to fix it
my directory structure look like below
thanks for any help

How do I return a template(thymeleaf) in spring boot and resolve it to a particular endpoint

Short: I want to use Thymeleaf template index.html but have the url point to thanks.html.
In depth: I am trying to have a form submission take my user to a page http://localhost:8080/thanks.html. I dont want the action of the form to be thanks.html for a few different reasons but I have greatly simplified the logic below. When all of the validation of the form are passed, I want to pass in a variable to indicate which layout to use. I have that working by using a model variable called contentPage. The problem is that if i have "return "thanks.html";" in the indexSubmit Method I get an error from thymeleaf saying template not found. If I change that to "return "index.html"; everything works but the url is http://localhost:8080/ instead of http://localhost:8080/thanks.html.
#PostMapping("/")
public String indexSubmit(Model model) {
model.asMap().clear();
model.addAttribute("contentPage","layout/thanks.html");
return "thanks.html";
}
#GetMapping("/thanks.html")
public String thanks(Model model) {
model.addAttribute("contentPage","layout/thanks.html");
return "index.html";
}
I fond an answer on my own:
return "redirect:thanks.html";
Thanks,
Brian

Laravel 4.2- How to Route to files in a Subfolder

This is my first time using stackoverflow. I am really stuck on a seemingly simple problem in Laravel 4.2, how to route to a bunch of files(.php view files in a subdirectory.
I have about forty .blade.php files in a subdirectory called mechanics.
When the clicks on the link
action('PagesController#mechanicspages') (Note: I don't know how to pass a value from here). The route is
Route::get('/mechanics/{id}', 'PagesController#mechanicspages');
The function at the PagesController is:
public function mechanicspages($id)
{
return View::make('/mechanics/{$id}');
}
Can I show a view with this logic?
To do this you need to use the find the object and send it to the view...
To access a view in a subfolder you just use a period "."
Here is what I would do:
Route file:
Route::get('/mechanics/{id}', 'PagesController#mechanicspages');
Controller File:
public function mechanicspages($id) {
$mechanic = Mechanics::find($id);
if($mechanic)
return View::make('mechanics.subview')->with($mechanic);
}
For more on this see:
How to pass data to view.
http://laravel.com/docs/4.2/responses (half way down. Search for "with")

RequestMapping with more than one path element

I'm trying to write the simplest possible Spring application which uses more than one path element in a #RequestMapping. For example, /appcontext/blog/hello-world.html should work in the request mapping (obviously /appcontext is my application's context).
Can Spring do something like that? I have it working easily when it maps just one thing. For example:
#RequestMapping("hello-world")
works and will match /hello-world.do , /anything/hello-world.do , but my problem is, I'm trying to match only hello-world if it's in a /blog path, and whenever I use something like:
#RequestMapping("/blog/hello-world")
it doesn't ever trigger.
Server log for example:
INFO: Mapped "{[/blog/hello],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mainweb.BlogController.foo()
That looks like it should work (and it's the only request mapping I have), but then:
WARNING: No mapping found for HTTP request with URI [/context/blog/hello.html] in DispatcherServlet with name 'dispatcher'
Can Spring do something like this? I don't want to put all my request mappings in /, because it's going to be a mess.
The only mapping I have gotten to work is:
#RequestMapping("/**")
From there, I could look at the HttpServletRequest object directly, but that seems to defeat the entire point of having #RequestMapping.
Here is my Controller:
#Controller
public class BlogController {
private static final Logger LOG = Logger.getLogger(BlogController.class.getName());
#RequestMapping("/blog/hello")
public String foo1() {
LOG.info("foo1");
return "nothing";
}
#RequestMapping("/blog/hello.html")
public String foo2() {
LOG.info("foo2");
return "nothing";
}
#RequestMapping("/blog/hello.*")
public String foo3() {
LOG.info("foo3");
return "nothing";
}
#RequestMapping("/blog/**")
public String foo4() {
LOG.info("foo4");
return "nothing";
}
#RequestMapping("/blog/{path}")
public String foo5(#PathVariable String path) {
LOG.info("foo5 " + path);
return "nothing";
}
// added this as a test - it's the only way that this controller works
#RequestMapping("/**")
public String foo6() {
LOG.info("foo6");
return "nothing";
}
}
If I don't have the foo6 mapping, nothing in this works at all, no matter what URLs I go to.
When I use that controller, this is what shows up in the server log:
INFO: Mapped "{[/blog/hello],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mainweb.BlogController.foo1()
INFO: Mapped "{[/blog/hello.html],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mainweb.BlogController.foo2()
INFO: Mapped "{[/blog/hello.*],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mainweb.BlogController.foo3()
INFO: Mapped "{[/blog/**],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mainweb.BlogController.foo4()
INFO: Mapped "{[/blog/{path}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mainweb.BlogController.foo5(java.lang.String)
INFO: Mapped "{[/**],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mainweb.BlogController.foo6()
But again, nothing ever triggers except foo6 if I put it in.
Thank you!
Edit: I have created an extremely simple project, which has only one class, the Controller, with one method, the RequestMapping. This project does nothing except not work. There is simply no way to get #RequestMapping to do anything other than work with a wildcard. This seems like a critical bug in Spring; all the documentations says that #RequestMapping does more than just map one wildcard.
It looks to me that the reason it's not mapping in the /blog path is that you're not using that path to access it.
According to the errors that you posted in the question above, you have defined/mapped:
/blog/hello
Whereas you are trying to access:
/context/blog/hello.html
i.e. You are prefixing the URI with "/context" and suffixing it with ".html". That's why it's not responding.
I would recommend trying to access the URI that you have mapped. However, you may also need to ensure that your application's root context is correct.
Note that by adding the ".html" suffix, you are not using the path that you defined. If you wish to use random suffixes such as that, then it is also possible to use wildrcards in your mappings. i.e. #RequestMapping(value = "/blog/hello**")
However, I would generally recommend against using wildcards if you can avoid it, as they are likely to throw up a few surprises when you add more mappings to your application.
Edit - I also just spotted that your controller is messing up those mappings even more. Just read the following mappings together:
#RequestMapping("/blog/hello")
#RequestMapping("/blog/hello.html")
#RequestMapping("/blog/hello.*")
Your mappings are clashing with each other. The first mapping in that list is a subset of the others, so it will be used for any URLs that match any of those mappings. i.e. The second 2 are redundant. You could potentially fix that by putting the 'bare' /blog/hello after the the other 2, so that the more specific mappings are picked up first, however, relying on the order in which the methods are written seems like asking for trouble to me.
Figured it out. The servlet-mapping chops off part of the path! The servlet-mapping basically creates a mini-context. I didn't know it did that, and it's not intuitively obvious, but that's what's happening.

Error "A namespace does not directly contain members such as fields or methods"

I'm trying to build my C# project and I'm getting the error message "A namespace does not directly contain members such as fields or methods". It is flagging the first character (the less than symbol) of the app.config file.
I've checked all of my files for places where there are variables or functions directly inside of a namespace--found nothing. The app.config looks fine.
Google is failing me and I'm pulling my hair out. What could be causing this error?
Figures! As soon as I finally break down and ask the question that I find the answer...
The app.config file properties section (somehow) listed the Build Action of "Compile" when it should be set to "None".
How in the world did it get changed? I know I didn't change it. Grrr...
Oh well, at least it's building now. Hopefully someone else will benefit from my hairloss.
I managed to reproduce the error.
Check the properties for the app.config file. The Build Action should be None, not Compile.
This error occurs when you attempt to add a field or member directly into a namespace. Given it's pointing to app.config it's likely that VS is in a bad state. Try closing and reopening Visual Studio and see if the error goes away.
Failing that try rebuilding and posting the results from the build output window to your question.
I solved this way: right click on .axml file -> Build Action ->AndroidResource
(xamarin studio)
I had the same error. Checked if the app.config was set to none, no problems there. After 30 minutes of checking and rechecking, I restarted VS and that did the thing. It usually solves 90% of the unlogical errors. Sighs.
I was having a similar problem with the code behind in my website.
The issue turned out to be an extra closing bracket that caused my class to end right after page_load(). Always check for silly syntax errors like this one.
I was facing this same issue with XML file. This was due to .net compiler tried to compile this xml which has been used only for DATA purpose hence no need of compilation.
Fix : Go to property of this file and change the Build Action to content from compile.
Thanks
In my case i was by mistake putting a new function outside the controller class.
This was my code in ASP.NET MVC that was giving this error.
public class HomeController : Controller
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
[HttpPost]
public string Insert(string name, string age, string standard, string percent, string address, string status)
{
//some code
return "value";
}
}
It should be like:
public class HomeController : Controller
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public string Insert(string name, string age, string standard, string percent, string address, string status)
{
//some code
return "value";
}
}
}

Resources