I don't see any prevailing errors with my code and the configuration and dependancies should all be correct, however when I search in the url the console outputs this:
"Path with "WEB-INF" or "META-INF": [WEB-INF/views/login.jsp]"
rather than opening the jsp file.
Does anyone why this is happening?
This is the structure of my folders within the project
Controller:
package com.example.mywebsite.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class LoginController {
#RequestMapping("/")
public String start(){
return "login";
}
}
Configuration:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
build.gradle:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'org.apache.tomcat.embed:tomcat-embed-jasper:9.0.41' // JSPs
runtimeOnly 'javax.servlet:jstl:1.2' // helpful JSP tags
runtimeOnly 'mysql:mysql-connector-java' //connect to sql
}
login.jsp:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
</body>
</html>
Try to remove the version number from tomcat jasper dependency in build.gradle fie.
For more details check the comments under this post
Related
I wonder why I have to add "classpath:" always int he resourcelocations mapping for Spring, is this best practice?
Currently my project is setup like this, but I if I remove the "classpath", it does not find anything (and also I cannot provide a proper direct link). For integating bootstrap, I even had to use "webjars-locator" to make webjars accessible (due to missing transparency).
Can anyone explain, why classpath is required (and no direct link), and how I can find or restrucutre the project to make it work properly?
ResourceHandlers:
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler(
"/webjars/**",
"/img/**",
"/css/**",
"/js/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/",
"classpath:/static/img/",
"classpath:/static/css/",
"classpath:/static/js/");
}
index.htm:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" th:href="#{/css/style.css}" />
<link rel="stylesheet" type="text/css" th:href="#{/webjars/bootstrap/4.2.1/css/bootstrap.min.css}" />
</head>
<body>
<h1 class="color1">Hello, world! style</h1>
<p>
Click <a th:href="#{/hello}">here</a> to see a greeting.
</p>
<!-- include javascript in the footer -->
**<script type="text/javascript" th:src="#{/webjars/jquery/3.2.1/jquery.min.js}"></script>
<script type="text/javascript" th:src="#{/webjars/bootstrap/4.2.1/js/bootstrap.min.js}"></script>**
</body>
</html>
It is normal to use classpath. If you want to additionally load resources from root path use
{"/", "classpath:/META-INF/resources/webjars/" ...}
I am trying to use Picklist from Primefaces. When the page is rendered, the javascript engine in Chrome cannot find Primefaces object. I get the following error 'Uncaught ReferenceError: PrimeFaces is not defined'. Am I missing to include any resource (js) in my .xhtml file? Please advise. Thanks.
Xhtml
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<link rel="stylesheet" type="text/css"
href="../jquery-ui-1.8.23.custom/css/ui-lightness/jquery-ui-1.8.23.custom.css" />
<link rel="stylesheet" type="text/css" href="css/form.css" media="all" />
<script src="../jquery-ui-1.8.23.custom/js/jquery-1.8.0.min.js"></script>
<script
src="../jquery-ui-1.8.23.custom/js/jquery-ui-1.8.23.custom.min.js"></script>
</head>
<h:body>
<form id="form_486588" class="appnitro" method="post">
<div class="form_description"></div>
<p:pickList id="pickList" value="#{editorsMB.editors}" var="editor"
itemLabel="#{editor}" itemValue="#{editor}" />
<p:commandButton id="citySubmit" value="Submit"
style="margin-top:5px" />
</form>
</h:body>
</html>
Managed bean
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.DualListModel;
#ManagedBean(name = "editorsMB")
#SessionScoped
public class AdminEditorsBean {
private DualListModel<String> editors;
public AdminEditorsBean(){
List<String> adminsSource = new ArrayList<String>();
List<String> adminsTarget = new ArrayList<String>();
adminsSource.add("aaa");
adminsTarget.add("target1");
editors = new DualListModel<String>(adminsSource, adminsTarget);
}
public DualListModel<String> getEditors() {
return editors;
}
public void setEditors(DualListModel<String> editors) {
this.editors = editors;
}
}
PrimeFaces will auto-include the necessary JS/CSS resources in <h:head>.
However, you don't have that tag. You've there a "plain HTML" <head>. Fix it accordingly:
<html>
<h:head>
...
</h:head>
<h:body>
...
</h:body>
</html>
This mistake should also have been logged in the server log as below:
One or more resources has the target of 'head' but not 'head' component has been defined within the view
See also How to include another XHTML in XHTML using JSF 2.0 Facelets? and When to use <ui:include>, tag files, composite components and/or custom components? for various correct examples of authoring JSF/Facelets pages.
Unrelated to the concrete problem: PrimeFaces as being a jQuery based JSF component library already ships with jQuery and jQuery UI bundled. You should remove the manually included scripts from your page to avoid conflicts. See also Adding jQuery to PrimeFaces results in Uncaught TypeError over all place.
Take immediately the opportunity to replace that verbose <link> by a <h:outputStylesheet>. See also How to reference CSS / JS / image resource in Facelets template?
I have a simple Spring boot application that is deployed to Jboss 6.4. But the JSP renders as text: Here are some details about the application:
Pom file dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
WebMvcConfig File has the following method:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import rg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
#Configuration
#EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
resolver.setPrefix("WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
JSP File:
<!DOCTYPE html>
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%# taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<html>
<head>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
</head>
<body>
Message: Test Message
</body>
</html>
But once deployed and accessed via Jboss here is what is displayed on the screen:
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%# taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Message: Test Message
Please let me know if any of you have come across issues with Spring Boot deployment into jboss and if there is any workaround.
Thank you in advance!
I have a Spring MVC controller which maps test to test.jsp. It has ModelMap object which has to be accessed by JSP page.
Here is controller
#RequestMapping(value = "/test")
public String test(ModelMap map)
{
map.addAttribute("data","the string which should bbe printed");
return "test";
}
Here is test.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<c:out value="${data}" />
</body>
</html>
I am getting nothing on the screen.
Please help me out.
I got the following error:
> org.apache.jasper.JasperException: An
> exception occurred processing JSP page
> /ajax/busstop_ajax.jsp at line 12
10: <%#taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
11: <%#taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
12: <f:loadBundle basename="/../messages.Messages" var="msg" />
13:
14: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
15: "http://www.w3.org/TR/html4/loose.dtd">
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
java.lang.NullPointerException
com.sun.faces.taglib.jsf_core.LoadBundleTag.doStartTag(LoadBundleTag.java:148)
org.apache.jsp.ajax.busstop_005fajax_jsp._jspx_meth_f_005floadBundle_005f0(busstop_005fajax_jsp.java:184)
org.apache.jsp.ajax.busstop_005fajax_jsp._jspService(busstop_005fajax_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
I'm trying to add a bundle to be able to translate the interface. The whole page is in jsp rather than html.
<%#page import="java.util.List"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%#taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<f:loadBundle basename="messages.Messages" var="msg" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSP Page</title>
</head>
<body>
...
</body>
</html>
</f:view>
I tried to add it in faces-config as well but no success. What am I doing wrong here?
Thanks for help!
The <f:loadBundle> will throw NPE like that when the FacesContext is not present.
In other words, you aren't invoking the request through the url-pattern of the FacesServlet as definied in web.xml. The FacesServlet is namely the one who is responsible for creating the FacesContext. If the url-pattern is for example *.jsf, you should open the page in webbrowser by pagename.jsf and thus not by pagename.jsp.
The stacktrace by the way also gives evidence that it's not been processed by JSF at all. There's no single line indicating involvement of the JSF API or impl prior to calling the f:loadBundle.
That said (and unrelated to the current problem), given the fact that you're using JSF 2.0 (at least, you tagged like that) and that Facelets is the superior successor of JSP -in case of JSF-, I strongly recommend to drop JSP altogether and use Facelets instead.