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.
Related
JSP is not getting parsed on browser. Instead show the directive directly.
Can anyone help me out
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>
Sample
</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button id="generateTicket">Generate Ticket</button>
<div id='ticket'></div>
<script>
$("#generateTicket").click(
function(){
console.log("Generate ticket");
$("#ticket").load("generateTicket");
});
</script>
</body>
</html>
below is the image of pom.xm
I have tried 2 ways to load external css or js files in my spring project but neither of them is working fine.
Home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css"
href="/resources/css/font-awesome.min.css" />//NOT LOADING
<link rel="stylesheet" href="<c:url value="resources/css/style.css"/>" />//Not loading Method tried 1
<link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="/resources/css/style.css">//Not loading Method tried 2
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
I have a Controller like this:
#Controller
public class HelloController {
#Autowired
private SomeService someService;
#RequestMapping("/")
public ModelAndView index() {
ModelAndView mav = new ModelAndView("index");
mav.addObject("title", someService.getTitle());
mav.addObject("text", someService.getText());
return mav;
}
}
Now thymeleaf is supposed to have some knowledge about the mav:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>${title}</h1>
<h2>${text}</h2>
</body>
</html>
But in my frontend it doesn't work and outputs the template vars:
${title}
${text}
Any ideas? Am I missing some configuration about the ViewResolver in Spring?
Fixed:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title th:text="${title}"></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1 th:text="${title}"></h1>
<h2 th:text="${text}">My Dummy Data</h2>
</body>
</html>
I realize this is a 4 year old question, and Stefano's answer is correct. But this is also correct (inlining):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="de">
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>[[${title}]]</h1>
<h2>[[${text}]]</h2>
</body>
</html>
I'm unable to pass variables from a.jsp to b.jsp, following is what I tried and the output. Can someone please help? Thanks.
a.jsp
start including
<jsp:include page="b.jsp">
<jsp:param name="somevar" value="zzzz" />
</jsp:include>
stop including
b.jsp
${somevar}
${param.somevar}
<%=request.getParameter("somevar")%>
output
start including
null
stop including
You have to set your param before include.
start including
<jsp:params>
<jsp:param name="somevar" value="zzzz" />
</jsp:params>
<jsp:include page="b.jsp">
</jsp:include>
stop including
And can u try this in b.jsp:
<%=request.getParameter("somevar");%>
What is output?
Try the following code:
home.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello world!</h1>
<jsp:include page="b.jsp">
<jsp:param name="something" value="something" />
</jsp:include>
<h1>Bye World</h1>
</body>
</html>
In the b.jsp page use the following code.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Param example</title>
</head>
<body>
<h1>
<c:out value="${param.something}" />
</h1>
</body>
</html>
The output must be
Hello world!
something
Bye World
Try to avoid java code in JSP pages as it is not advisable. For more details, have a look at this answer How to avoid Java Code in JSP-Files?
I am trying as explained here, but couldn't get the output that is shown for the second page.
It redirects to the second page but doesn't print the text i entered, rather returns the empty page.
Any suggestions ? i am using spring framework 3.0.2.
helloView.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h1>${helloMessage}</h1>
</body>
</html>
nameView.jsp
<%#taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Name</title>
</head>
<body>
<h1>Enter your Name</h1>
<spring:nestedPath path="name">
<form action="" method="post">
Name:
<spring:bind path="value">
<input type="text" name="${status.expression}" value="${status.value}">
</spring:bind>
<input type="submit" value="OK">
</form>
</spring:nestedPath>
</body>
</html>
Add this bean class
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
This bean is responsible for automatically creating an URL mapping for all controllers registered in the file. It takes the fully-qualified class name of the controller (in our case, controller.HelloController) and strips the package name and Controller suffix, then uses the result as a URL mapping.