I'm writing a wizard for creating a user in my application with Spring MVC. At each step the controller will set session attributes for the completed wizard fields.
I want the wizard to look the same regardless of which page it's on, except for each page's fields, obviously. For example, menus and links at the top of the page and buttons at the bottom should remain the same.
I have the following JSP
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Create a new User</title>
</head>
<body>
<h1>User Creation Wizard</h1>
Step <c:out value = "${pageNum}"/>/<c:out value = "${pageMax}"/>
<form action="" method="POST">
<jsp:include page="userform${pageView}.jsp"/>
<input name = "currentPage" type = "hidden" value = "${pageNum}"/>
<c:if test = "${pageNum > 1}">
<input name = "prev" type = "submit" value = "Previous" />
</c:if>
<c:if test = "${pageNum < pageMax}">
<input name = "next" type = "submit" value = "Next" />
</c:if>
<c:if test = "${pageNum == pageMax}">
<input name = "submit" type = "submit" value = "Finish" />
</c:if>
</form>
</body>
</html>
In the jsp I'm including, do I need to remove the <html>, <head>, and <body> tags? The above code is based on this example.
Yes, you'd need to remove the <html>, <head> and <body> tags from the included JSP file. As they'd already be present in the including file keeping them would result in invalid HTML.
Only the content that you want to vary would be in the JSP file you're including. Everything else, including the necessary <html>, <head> and <body> tags, would be in the JSP file that does the including.
Related
I want to send a query string from one jsp page to jsp page but I want to hide the name-value pairs(attributes) at address bar when I send the query string.
First.jsp
<%# 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>First Page</title>
</head>
<body>
Click Here
</body>
</html>
Second.jsp
<%# 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Second Page</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
%>
Username : <%=username %><br/>
Password : <%=password %><br/>
</body>
</html>
Here, I pass a query string "Second.jsp?username=aditya123&password=abc12345" from First.jsp page to Second.jsp page but I want to send this without showing username and password attribute and their value at address bar.How can it possible?
Try this code
<form action="some.jsp" method="post">
<input type="text" name="uid" >
<input type="password" name="pass">
<input type="submit" name="login" >
</form>
Adding method ="post" hides the query stringThat is ,if i remove ' method="post" ' the processed url on pressing submit button would be having
Following as query string
uid="whatever i wrote in text field"&pass=""&login="Submit"
But after writing ' method="post" ' the new url will be free of query string...!
it is not possible with link.
alternate solution of not showing attribute is encode that name value pair and send it with url and decode at another page.
use either url encoder given by java or make use of your own encrypt-decrypt method.
The easiest thing to do is <form action="some.jsp" method="post">
Do this formatting in the html code.
And contents of url will be hidden...
You can store all the objects/information you want to pass to the second jsp file by storing the objects in the 'session' implicit object using session.setAttribute() method. In the second page you can retrieve those objects from the 'session' object using session.getAttribute(). My assumption here is that both the jsp pages are being executed in the same HttpSession, hence the same 'session' object will be available to both the jsp pages.
I'm not understanding how to set up an href to redirect to another jsp. This is what I have so far.
JSP with a href in it:
<%# 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<h1>${gradebook.gradeBookName} was created.</h1>
<br> Home Page <br>
Add grades to GradeBook
</div>
</body>
</html>
The href "/createGradeBook3" is the link I want to re-direct to.
Controller:
#RequestMapping(value = "/viewGradeBook3", method = RequestMethod.GET)
public String viewGradeBook3(Locale locale, Model model)
{
return "viewGradeBook3";
}
Try to use the following code snippet in your controller -
return "redirect:/path/to/jsp";
Or you may use another URL that has been mapped with a controller's handler method. In this case the redirected request will be handled by the controller's handler method.
Hope it will help.
Thanks a lot.
This question already has an answer here:
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
(1 answer)
Closed 7 years ago.
I have a facultylist.jsp page which displays List<Faculty> as a request attribute parameter in forEach loop and I want every item in this loop to be a link to specified faculty facultyview.jsp. How can I achieve that ?
facultylist.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Faculties</title>
</head>
<body>
<h1>Faculties list</h1>
<ul>
<c:forEach var="faculty" items="${faculties}">
<li>${faculty.name}</li>
</c:forEach>
</ul>
</body>
</html>
facultyview.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Faculty</title>
</head>
<body>
<h1>${faculty.name}</h1>
<ul>
<li>Faculty name: <c:out value="${requestScope.name}"></c:out></li>
<li>Total seats: <c:out value="${requestScope.total_seats}"></c:out></li>
<li>Budget seats: <c:out value="${requestScope.budget_seats}"></c:out></li>
</ul>
apply for this faculty
</body>
</html>
I don't know if its may help, but I'm using following technologies: tomcat, jsp, servlets and log4j.In my project I have one FrontController, which is a servlet that interacts with Command pattern - each Command returns a path to resource and action type: forward or redirect.
You can solve your issue by adding a query params to the link, edit with respect to the comment. Note that you cannot access directly the JSP pages that reside under WEB-INF folder. Also, to encode properly the paramters, better construct url like
<c:url value="facultyview.jsp" var="url">
<c:param name="name" value="${faculty.name}"/>
<c:param name="total_seats" value="${faculty.total_seats}"/>
<c:param name="budget_seats" value="${faculty.budget_seats}"/>
</c:url>
<li>${faculty.name}</li>
and than than in your facultyview.jsp read from the query params
<li>Faculty name: ${param.name}</li>
<li>Total seats: ${param.total_seats}</li>
<li>Budget seats:${param.budget_seats}</li>
This direct JSP communication should solve your immediate issue, but a truly proper way would be to pass an id of a faculty to servlet, fetch the faculty instance, place in the model and pass to the view.
in other way is just take the selected value from the drop down with a name and forward it to front controller servlet ,there use if else conditions and depends on the value you could forward the request to corresponding jsp or servlet
<select name="value"> in jsp
String value=req.getParameter("value"); in servlet
if()
else if()
If you have a field in Faculty entity simply:
${faculty.name}
#Mark: faculty represents an entity from database, i'm not sure if I want to change it adding another field, or you mean some other way ?
Add a field does not means you must change database, you can have a Helper entity that inherits from Faculty and have more fields you can need,
public class FacultyFormHelper extends Faculty implements Serializable {
private String URL;
and in your view:
${facultyHelper.name}
But, If you don't want to modify your database, either create a helper class, you may add onclick event to the <a>
<a onclick="goToURL(${faculty.id})">
Then retrieve the data... i'm not sure how you get the urls... from a variable in the view, ajax call or wherever you have this URL...
I have a html page with a radiobuttonlist.Based on the user's input on that I need to provide various input options..I have created JSP pages for the various options and was trying to use javascript.I am able to pass the input checked to the js script but couldnt proceed furthur.I am using struts2 framework.Can someone suggest some way of doing it
<%# 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">
<%#taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<script language="JavaScript" type="text/JavaScript">
function getvalues(input){
document.write(input);}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register Page</title>
</head>
<body>
<s:form action="Register">
<s:select name="product" list="productList" listKey="productName"
listValue="productName" headerKey="0" headerValue="Select"
label="Select a product" />
<s:radio name="option" label="Select Change:" list="{'option1','option2','option3'}" onclick="getvalues(this.value)" />
<s:submit />
You can show different JSP's based on your selected value.You can simply call you action class and return results based on your inputs.
JavaScript
function getvalues(input){
$.ajax({
type : 'GET',
url : you action URL+'?input='+input
success : function(response) {
// show content on success
},
error : function(e) {
}
});
}
Action Class
public class MyAction extends ActionSupport{
private String input;
private String view;
// getter and setter for view and input
public String execute() throws Exception {
//use input to determine which JSP you want to show
setView(JSP you want to show based on value of input);
return SUCCESS;
}
}
Struts.xml
<action name="Your Action" class="Your Class">
<result>${view}</result>
</action>
Here i am using ${view} using as dynamic value as this is being set in your action class and based on the value you have used in your action class, corresponding JSP will be picked by S2 to render your view.
Learning spring + jsp.
I am setting a model attribute in a method in a controller
#RequestMapping("/viewExpenses")
public String viewAllExpenses(Model model){
List<Expense> expenses;
expenses = expenseService.getAllExpenses();
model.addAttribute(expenses);
for(Expense e : expenses)
System.out.println(e);
return "viewExpenses";
}
where expenses size is > 1
Following is my jsp page:
<%# taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%# taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h2>
Hello ${user.firstName} <br> HOME
</h2>
<div>All Expenses</div>
<span style=""></span>
<c:forEach var="expense" items="${expenses}">
<c:out value="${expense.expenseId}"></c:out>
<c:out value="${expense.expenseName}"></c:out>
<c:out value="${expense.expenseType}"></c:out>
<c:out value="${expense.expensePrice}"></c:out>
<c:out value="${expense.purchaseDate}"></c:out>
<c:out value="${expense.expenseComments}"></c:out>
</c:forEach>
</body>
</html>
Following is my output from the jsp(no errors):
Hello Pravat
HOME
All Expenses
I wonder why the expenses are not populated in my jsp
You need to give your attribute an attribute name:
model.addAttribute("expenses", expenses);
Background:
When a name is not supplied, Spring uses a generated name which is not the same as the variable name. The docs explain this:
Determine the conventional variable name for the supplied Object based on its concrete type. The convention used is to return the uncapitalized short name of the Class, according to JavaBeans property naming rules: So, com.myapp.Product becomes product; com.myapp.MyProduct becomes myProduct; com.myapp.UKProduct becomes UKProduct.
Give it a name
model.addAttribute("expenses", expenses);
Return ModelAndView.
Always enclose body of if/for/etc into { } even it's single-lined.