I'm new to Spring Boot, I have files under webapp resource folder, login.xhtml, index.xhtml, register.xhtml they are working fine with http://localhost:8080/login.xhtml, http://localhost:8080/register.xhtml, http://localhost:8080/index.xhtml but they are not working without the file extension, http://localhost:8080/login, http://localhost:8080/index, http://localhost:8080/register gives error for /login.
Request URI: /login Ajax request: No Status code: 500 Exception type:
class org.thymeleaf.exceptions.TemplateInputException Message: Request
processing failed; nested exception is
org.thymeleaf.exceptions.TemplateInputException: Error resolving
template [login], template might not exist or might not be accessible
by any of the configured Template Resolvers
My application.properties is configured propertly:
spring.thymeleaf.prefix=classpath:/webapp/
spring.thymeleaf.suffix=.xhtml
Controller for login:
public class LoginController {
#GetMapping("/login")
public String login()
{
return "login";
}
}
I wan to access both with extension and without extension. localhost:8080/login and localhost:8080/login.xhtml should work.
you comments and solutions could solve my problem.
You can use many path links to a single controller action, e.g.
#RequestMapping(path = {"/login", "/login.xhtml"})
public String login() {
return "login";
}
Also, don't forget to add #Controller annotation to your class.
I'm building my first rest api webservice and now i want to get two different path params, but it is not working!
here's the code that is causing the error:
#PUT
#Path("/{projectid}/updateproject/{profileid}")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Projects updateProject(
#PathParam("projectid") int projectid,
#PathParam("profileid") int profileid,
Projects pProject,
Profiles pProfile) {
pProject.setProjektid(projectid);
pProfile.setPersonalnummer(profileid);
return service.addProfileInProject(pProject, pProfile);
}
And this is the error i get:
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
It also says something with:
on resource class ... contains multiple parameters with no annotation. Unable to resolve the injection source.
Please help me with this issue!
Thanks! <3
I have a Java/Spring web application.
Controller code that shows the view looks like this:
#RequestMapping(value = { "showmessage/{messageId}" }, method = RequestMethod.GET)
public ModelAndView viewMessage(Model model, #PathVariable("messageId") Integer messageId) throws Exception {
/* do stuff */
return new ModelAndView("show-message","message",message);
}
Which displays the contents of the show-message.jsp file.
The problem is, the show-message.jsp file is trying to load resources that are in the myapp.WAR file ... for example ...
/resources/dist/js/jquery/jquery-validate-1.17.0/jquery.validate.js
The browser reports that it failed to load this from this URL ...
http://localhost:8080/myapp/showmessage/resources/dist/js/jquery/jquery-validate-1.17.0/jquery.validate.js
... because the resource is actually here ...
http://localhost:8080/myapp/resources/dist/js/jquery/jquery-validate-1.17.0/jquery.validate.js
If I change the resource line in the JSP from "resources/dist/..." to SLASH "/resources/dist/..." is still doesn't work because it tries load this ...
http://localhost:8080/resources/dist/js/jquery/jquery-validate-1.17.0/jquery.validate.js
... and fails.
What's wrong with my app and how do I make this work?
In you JSP you are most likely using a relative href values e.g.:
<scripts src="resources/dist/js/jquery/jquery-validate-1.17.0/jquery.validate.js"></script>
The browser will resolve it against the current URL which is showmessage/{messageId} based on your #RequestMapping annotation.
Try specifying an absolute URL to your script. Since you have JSP you can use <c:url>.
<scripts src="<c:url value="/resources/dist/js/jquery/jquery-validate-1.17.0/jquery.validate.js"/>"></script>
i have tried to write an upload option in my web application following mentioned is my code
#RequestMapping( value="/group/import/upload", method=RequestMethod.GET )
public String GroupImportInfo( Model model )
{
System.out.println("in controller");
return "upload-File-import";
}
i have a upload-File-import.jsp file, but when i try to access that page am getting an exception saying..
type Status report
message /corept/WEB-INF/views/upload-File-import.jsp
description The requested resource (/corept/WEB-INF/views/upload-File-import.jsp) is not available.
JBoss Web/7.0.13.Final
Thanks for helping.
It seems the ViewResolver is unable to locate the view. Hence, I suggest you to check configuration of the ViewResolver bean's prefix property(in the spring-servlet-config.xml) if it is matching the path of the requested view.
Apart from that I dont see any issue with your code.
I have two pages: home.jsp, and stylechoosertable.jsp. home.jsp has a simple link going to stylechoosertable.jsp. Both are in src - main - webapp - views. Starting the app runs fine, loading home.jsp is fine. However, when I click the link, it goes to a 404 Not Found page.
Here is HomeController.java
#Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
logger.info("Welcome home! The client locale is {}.");
return "home";
}
}
Here is CSSTableController.java
#Controller
public class CSSTableController {
private static final Logger logger = LoggerFactory.getLogger(CSSTableController.class);
private final CSSService cssService;
#Autowired
public CSSTableController(CSSService cssService) {
this.cssService = cssService;
}
#RequestMapping(value = "/stylechoosertable.jsp", method = RequestMethod.GET)
public List<StyleChooser> get() {
return cssService.getAllStyleChoosers();
}
}
servlet-context.xml
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
web.xml
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
A couple of things I've noticed:
Only local:8080/myspringmvc/ brings up the home page. local:8080/myspringmvc/home.jsp brings up the same 404 error, which I don't get.
On start, I can see it doing the Request/Handler mappings properly:
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/stylechoosertable.jsp],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.List com.css.genapp.CSSTableController.get()
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.css.genapp.HomeController.home(org.springframework.ui.Model)
It doesn't throw any org.springframework.web.servlet.PageNotFound errors when I go to home.jsp or stylechoosertable.jsp, so I know the mapping is there.
I can't figure out why it doesn't "hit" my stylechoosertable.jsp. Any ideas?
Well I don't think I am able to get your question properly, still if you want to have simple navigation function from home.jsp, i.e on click of the link, it should go to stylechoosertable.jsp, then a definite solution would be just have a link like
SomeName
Now simply map this in controller
#RequestMapping(value = "/stylechoosertable")
public String goToSCT() {
return "stylechoosertable";
}
And if there is any stylechoosertable.jsp available in /WEB-INF/views/, it will simply get called.
Second Question the reason behind local:8080/myspringmvc/ working, not local:8080/myspringmvc/home.jsp is basically lack of RequestMapping for /home.jsp, where as for the root URL i.e /, a mapping is there in HomeController
Most Servlet containers register a Servlet implementation to handle JSPs. They would look like
<servlet>
<servlet-name>jspServlet</servlet-name>
<servlet-class>com.example.JspServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jspServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
The *.jsp is known as an extension mapping. A Servlet container knows about this Servlet and the ones you've registered. The container doesn't know anything about your #Controller beans. That are relevant to Spring only.
When a Servlet container receives a request, it goes through an order specified in the Servlet specification for matching a particular Servlet based on its mapping(s). Basically, it first checks for an exact match, then it tries to do path matching (patterns like /something/*), then it does extension mapping, and finally, if no matches were found, uses the default servlet, a servlet mapped to /. In this case, your DispatcherServlet (which runs the Spring MVC stack) is mapped to /, so it is the default servlet.
When you send your request to
local:8080/myspringmvc/home.jsp
the path satisfies the extension mapping of *.jsp and therefore the container uses the JspServlet to handle the request. This JspServlet tries to find an appropriate resource, but doesn't. Remember that JSPs inside WEB-INF are not accessible publicly/externally. It therefore returns a 404.
When you send your request to
local:8080/myspringmvc/
the default servlet is chosen because nothing else can match that path. The DispatcherServlet looks up its handlers and finds
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
logger.info("Welcome home! The client locale is {}.");
return "home";
}
By returning the view name home and because you've registered the appropriate InternalResourceViewResolver, Spring will do a RequestDispatcher#forward(..) to that JSP. That request will again be matched by the Servlet container, but this time, to the JspServlet. Because this is an internal call (and the path is right), the request will be completed.
Similarly, for /stylechoosertable.jsp, the JspServlet is chosen before your DispatcherServlet could do anything about it.
It doesn't throw any org.springframework.web.servlet.PageNotFound
errors when I go to home.jsp or stylechoosertable.jsp, so I know the
mapping is there.
The DispatcherServlet is never involved, so it doesn't have the chance to log anything.