I'm going through this Spring tutorial online form springsource.org.
http://static.springsource.org/docs/Spring-MVC-step-by-step/part2.html
In Chapter 2, at the end, it has you add a bean to prefix and suffix /WEB-INF/jsp/ and .jsp to responses.
The code so far should basically load index.jsp when you go to localhost:8080/springapp/ which will redirect to localhost:8080/springapp/hello.htm which creates an instance of the HelloController which should in theory send you over to /WEB-INF/jsp/hello.jsp. When I added the prefix/suffix bean and changed all my references to just "hello" instead of the fully pathed jsp file, I started getting the following error:
message Handler processing failed; nested exception is
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/fmt/LocalizationContext
I've tried going back through the samples several times and checking for typo's and I still can't find the problem. Any tips or pointers?
index.jsp (in the root of the webapp:
<%# include file="/WEB-INF/jsp/include.jsp" %>
<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
<c:redirect url="/hello.htm" />
HelloController.java (minus the imports and package:
public class HelloController implements Controller {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new Date()).toString();
logger.info("Returning hello view with " + now);
return new ModelAndView("hello", "now", now);
}
}
My hello.jsp file:
<%# include file="/WEB-INF/jsp/include.jsp" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello :: Spring Application</title>
</head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings, it is now <c:out value="${now}" /></p>
</body>
</html>
It seems like you are missing the JSTL jar here. Try downloading it and place it in your classpath to see if it works: Where can I download JSTL jar
It seems certain required jar(s) are missing from classpath.
Make sure you have servlet-api-2.x.jar jsp-api-2.x.jar and jstl-1.x.jar on classpath
Please make sure the jstl.jar file is located in your WEB-INF/lib folder.
As a matter of fact, here is what is stated in the tutorial that you linked. I guess you missed this step:
We will be using the JSP Standard Tag Library (JSTL), so let's start
by copying the JSTL files we need to our 'WEB-INF/lib' directory. Copy
jstl.jar from the 'spring-framework-2.5/lib/j2ee' directory and
standard.jar from the 'spring-framework-2.5/lib/jakarta-taglibs'
directory to the 'springapp/war/WEB-INF/lib' directory.
Related
I am very new to Spring Boot framework and want to clarify why I am facing with this issue.
Issue: .jsp file is not shown at the correct endpoint.
this is my controller class
#Controller
public class HomeController {
#RequestMapping("home")
public String home() {
System.out.println("Hello World");
return "home.jsp";
}
}
This is my application.properties class
spring.mvc.view.prefix = /webapp/
spring.mvc.view.suffix = .jsp
this is what I have inside home.jsp file
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Change Titlte</title>
</head>
<body>
Hello
</body>
</html>
and lastly, the project directory
When I start spring boot app the site looks like this
Why do you think I can't see the content of home.jsp file?
Any help is appreciated.
Best,
PS: I already added Tomcat Jasper libraries in the pom.xml file
Check this simple examle. It seems you should not use .jsp extension in controller's return statement. Don't forget to specify method of your endpoint. Use #GetMapping instead of #RequestMapping
Also you'd better switch to Spring MVC thymeleaf that works good with html files. jsp - is used mostly for Java EE projects.
There are several things to notice here.
Change mapping to /home instead.
View resolver is configured already, return "home" instead of "home.jsp".
Take Model object as an argument it will be useful.
home(ModelMap model)
I'm trying to create and run a servlet in IntelliJ. The problem I'm having is I'm following an Eclipse tutorial and they seem to work very differently. In Eclipse, a servlet.java class is created and run on Tomcat. In IntelliJ a .java class and .jsp file is created. The browser points to .jsp, not .java. The java class doesn't seem to be doing anything at all.
Why are they so different, and how can I point to the .java class instead of the .jsp?
I've added the .java and .jsp code below, which are both the standard stubs created by IntelliJ when creating a new servlet project.
#WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "The Tomcat server does not point to this code in IntelliJ";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Tomcat points to this .jsp file, not the .java code" %>
</h1>
<br/>
Hello Servlet
</body>
</html>
The servlet class has an annotation indicating what URI to use, so whatever URL your application is accessed using, followed by /hello-servlet.
Kind of weird that they generate these files that'll be useless 99% of the time.
So I'm trying to just follow instructions for simple Spring Boot project using devtools+mustache+data-jpa. I'm just copy-pasting the whole thing and it doesn't work, even thought tutorial says "Just press the button and it works". Full source code is here, some listings I will provide in the end.
All I want to do is to redirect to index.html from localhost:8080/ and insert simple value into the template.
But instead:
1. Something redirects me from / to /apex/f?p=4950:1 for some reason
2. If I change mapping to #GetMapping("/home") and try localhost:8080/home I get 404
After enabling logging I found out that PathResourceResolver doesn't scan the /resources/templates directory. And if I add dependency on Thymeleaf, it finds it.
So the question is where is the problem? Should I add some config file? Or Mustache isn't working like that?
IndexController.java
#Controller
public class IndexController {
#GetMapping("/")
public ModelAndView home() {
Map<String, String> model = new HashMap<>();
model.put( "name", "Alex" );
return new ModelAndView( "index", model );
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Welcome to Spring, {{ name }}</h1>
</body>
</html>
Dependencies
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-mustache')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
Structure
Log
In order for given demo app to work, please add following to the main/resources/application.properties
spring.mustache.prefix=classpath:/templates/
spring.mustache.suffix=.html
This will tell Spring where to look for Mustache views and what extension those are supposed to have.
I am attempting to get to grips with a decent level of JSP quickly to build an interface. I found what seems a good tutorial at this URL.
Reference link
While following the tutorial to the word, on page 35 it covers jsp:getProperty Action. I successfully created the TestBean.java file and it compiled to produce TestBean.class. But what I found was that my Tomcat's webapps directory did not contain a folder named "WEB-INF" and so no directory "webapps\WEB-INF\classes\action" so I added the "WEB-INF\classes\action" myself. Then when I run the main.jsp file the browser states the following error
"The value for the useBean class attribute action.TestBean is invalid"
What I am doing wrong?
Here is the Java "TestBean.class" code;
/* File: TestBean.java */
package action;
public class TestBean {
private String message = "No message specified";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
this.message = message;
}
}
And here is the JSP "main.jsp" code
<html>
<head>
<title>Using JavaBeans in JSP</title>
</head>
<body>
<center>
<h2>Using JavaBeans in JSP</h2>
<jsp:useBean id="test" class="action.TestBean" />
<jsp:setProperty name="test" property="message" value="Hello JSP..." />
<p>Got message....</p>
<jsp:getProperty name="test" property="message" />
</center>
</body>
</html>
I am assuming you are working on eclipse.
If Web-INF is not created that means you are not working in a dynamic web project.
Go to NEW in eclipse. Go to Dynamic web project. It will create all you folders needed for a JSP/WEB project itself including Web-INF. This will stream line the process.
Other option is place your class files in required folder which is quite tedious job. Hope it helps.
I'm new to JSP, using Eclipse, and am trying to just get started with templates. I've imported template.tld into WebContent/WEB-INF/tlds.
Guide: http://www.javaworld.com/jw-09-2000/jw-0915-jspweb.html
When I run the test.jsp file, I get this error:
org.apache.jasper.JasperException: /test.jsp(3,0) Unable to load tag handler class "tags.templates.InsertTag" for tag "template:insert"
I've tried searching Google, but am unable to find a solution. Any help would be greatly appreciated!
template.jsp
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
<html><head><title><template:get name='title'/></title></head>
<body>
Welcome!<br />
<template:get name='content'/>
</body></html>
test.jsp
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
<template:insert template='/template.jsp'>
<template:put name='content' content='this is our website'/>
</template:insert>
Have you also added the taglib's classes to /WEB-INF/lib or /WEB-INF/classes? Just having the TLD is not enough, you need the code that actually implements it. Your error message fragment suggests you haven't done that.