SpringMVC model attribute returns null on post - java

I was developing a SpringMVC 3 trivial application, but I got stuck somewhere. Essentially, a model attribute whose fields are filled in the GET operation are returned NULL in the POST (even if I don't make any operation on them). I've checked on this and other forums and the only answer I came up with was to implement Editors for the classes I should put into the model, an initializer that could be used to register custom editors and make it available to the application (in the servletname-servlet.xml file). All operations that I did, but definitely no luck. I was wondering if someone out there could give me a hand.
Thank you.
The following controller:
#Controller
#RequestMapping(value="/nourish")
public class NourishController {
private PetDAO pdao = new PetDAO();
private UserDAO udao = new UserDAO();
private FeedVirtualPet feedvp = new FeedVirtualPet();
#RequestMapping(method = RequestMethod.GET)
public String nourish(Model model, HttpServletRequest request){
NourishPetDTO npdto = new NourishPetDTO();
PetDTO pdto=pdao.findPetByBusinessKey((PetDTO)request.getSession().getAttribute("pet"));
npdto.setPet(pdto);
npdto.setAmt(0);
model.addAttribute("npdto", npdto);
return "nourish";
}
#RequestMapping(method = RequestMethod.POST)
public String nourishPOST(#ModelAttribute("npdto") NourishPetDTO npdto,
//BindingResult result,
HttpServletRequest request){
System.out.println("*****nourishPOST.npdto.amt: "+npdto.getAmt());
System.out.println("*****nourishPOST.npdto.pet.petname: "+npdto.getPet().getPetName());
System.out.println("*****nourishPOST.npdto.pet.hunger: "+npdto.getPet().getHunger());
PetDTO pdto = feedvp.feed(npdto.getPet(), npdto.getAmt());
request.getSession().setAttribute("user", pdto.getOwner());
return "redirect:detailPet";
}
}
has methods for both GET and POST operations, and is associated to the following jsp - in this view, all the model informations are correctly displayed through EL:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" session="true" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Nourish your pet!!</title>
</head>
<body>
Your stats for <h3>${npdto.pet.petName}</h3><br>
Please note that any value you put inside will:
<ol>
<li>Subtract value to your current hunger level</li>
<li>Add (value) to your current health level</li>
</ol>
Please note that any value you'll put will in many manners "resized":
<ol>
<li>It must be even. If not, a default 0 value will be applied</li>
<li>It can't be greater than 4. If it's greater, the maxium value of 4 will be anyway considered.</li>
<li>If it ain't a number, a default zero value will be passed</li>
</ol>
<table>
<tr><td>Health</td><td>${npdto.pet.health}</td></tr>
<tr><td>Hunger</td><td>${npdto.pet.hunger}</td></tr>
</table>
<form action="nourish" method="post" >
nourishment: <input type="text" name="amt"/>
<input type="submit" value="Nourish!"/>
</form>
</body>
</html>
(Please note how I didn't use the model attribute that's returning NULL, to be sure I wasn't doing anything to it)
The POST operations fail on the instruction
System.out.println("*****nourishPOST.npdto.pet.petname:"+npdto.getPet().getPetName());
as Tomcat returns a NullPointerException.
As aforementioned, I have been searching a solution to this problem, and everything I could find is to add Editor classes & register Editors to a binder. Result is still the same.
Anyway, these are the classes:
NourishPetEditor.java
public class NourishPetEditor extends PropertyEditorSupport {
private PetEditor pedit;
public PetEditor getPedit() {
return pedit;
}
public NourishPetEditor() {
// TODO Auto-generated constructor stub
pedit = new PetEditor();
}
#Override
public String getAsText(){
NourishPetDTO npdto= (NourishPetDTO)getValue();
return super.getAsText()+","+npdto.getAmt();
}
public NourishPetDTO makeNourishPetDTOInstance(String [] parts){
NourishPetDTO npdto = new NourishPetDTO();
npdto.setPet(pedit.makePetDTOInstance(parts));
npdto.setAmt(Integer.parseInt(parts[9]));
return npdto;
}
public void setAsText(String key){
String []parts = key.split(",");
NourishPetDTO npdto = makeNourishPetDTOInstance(parts);
setValue(npdto);
}
}
PetEditor.java
package com.virtualpet.dtoeditors;
import java.beans.PropertyEditorSupport;
import com.virtualpet.virtualpet_daos.PetDAO;
import com.virtualpet.virtualpet_dtos.PetDTO;
import com.virtualpet.virtualpet_dtos.UserDTO;
public class PetEditor extends PropertyEditorSupport{
private PetDAO pdao;
public PetEditor() {
// TODO Auto-generated constructor stub
}
public PetEditor(PetDAO pdao) {
// TODO Auto-generated constructor stub
this.pdao = pdao;
}
public String getAsText(){
PetDTO pdto = (PetDTO) this.getValue();
return pdto.getClass().getName()+","+ //0
pdto.getPetName()+","+ //1
pdto.getHealth()+","+ //2
pdto.getHunger()+","+ //3
pdto.getMood()+","+","+ //4
pdto.getOwner().getClass().getName()+","+ //5
pdto.getOwner().getUsername()+","+ //6
pdto.getOwner().getEmail()+","+pdto.getOwner().getPassword(); //7,8
}
public void setAsText(String key) throws IllegalArgumentException {
String []parts = key.split(",");
PetDTO pdto = makePetDTOInstance(parts);
setValue(pdto);
}
public UserDTO makeUserDTOInstance(String[] parts)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException {
UserDTO udto = (UserDTO)Class.forName(parts[5]).newInstance();
udto.setUsername(parts[6]);
udto.setEmail(parts[7]);
udto.setPassword(parts[8]);
return udto;
}
public PetDTO makePetDTOInstance(String[]parts){
try{
PetDTO pdto = (PetDTO) Class.forName(parts[0]).newInstance();
pdto.setPetName(parts[1]);
pdto.setHealth(Integer.parseInt(parts[2]));
pdto.setHunger(Integer.parseInt(parts[3]));
pdto.setMood(Integer.parseInt(parts[4]));
UserDTO udto = makeUserDTOInstance(parts);
pdto.setOwner(udto);
return pdto;
}
catch (Exception e){
throw new IllegalArgumentException();
}
}
}
I'll spare you UserEditor, as it's pretty much similar to PetEditor.
Finally, the Initializer to bind the custom editors & the Model Classes.
VirtualPetDTOInitializer.java
package com.virtualpet.dtoeditors;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
import com.virtualpet.virtualpet_dtos.NourishPetDTO;
import com.virtualpet.virtualpet_dtos.PetDTO;
import com.virtualpet.virtualpet_dtos.UserDTO;
public class VirtualPetDTOInitializer implements WebBindingInitializer {
public void initBinder(WebDataBinder binder, WebRequest arg1) {
// TODO Auto-generated method stub
binder.registerCustomEditor(UserDTO.class, new UserEditor( ));
binder.registerCustomEditor(PetDTO.class, new PetEditor( ));
binder.registerCustomEditor(NourishPetDTO.class, new NourishPetEditor());
}
}
This class defines a property value in dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<bean id="userDao"
class="com.virtualpet.virtualpet_daos.UserDAO"/>
<bean id="petDao"
class="com.virtualpet.virtualpet_daos.PetDAO" />
<bean class="org.springframwork.web.servlet.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.virtualpet.dtoeditors.VirtualPetDTOInitializer"/>
</property>
</bean>
</beans>
Being a total rookie on Spring MVC, I must tell you that this error is something I got even before I implemented these classes. So, looks like they're not a factor and yet, their implementation is everything I could find aboud model attribute returned null after POST.
Could anyone please help? Thanks in advance.

You need to do one of the following:
Store the values of npdto in hidden form fields
Store npdto in session
Re-read npdto from the database in your post handler
You probably want #2, in which case add #SessionAttributes("npdto") on top of your Controller.
You should also add a SessionStatus parameter to your post handler, and call sessionStatus.complete() to clear the item from session when you don't need it any more.
See Spring MVC: Validation, Post-Redirect-Get, Partial Updates, Optimistic Concurrency, Field Security for a reference answer.

Related

jsp page did not display arraylist values [duplicate]

I'm implementing MVC using JSP and JDBC. I have imported a database class file to my JSP file and I would like to show the data of a DB table. I don't know how I should return the ResultSet from the Java class to the JSP page and embed it in HTML.
How can I achieve this?
In a well designed MVC approach, the JSP file should not contain any line of Java code and the servlet class should not contain any line of JDBC code.
Assuming that you want to show a list of products in a webshop, the following code needs to be created.
A Product class representing a real world entity of a product, it should be just a Javabean.
public class Product {
private Long id;
private String name;
private String description;
private BigDecimal price;
// Add/generate getters/setters/c'tors/equals/hashcode boilerplate.
}
A DAO class which does all the nasty JDBC work and returns a nice List<Product>.
public class ProductDAO {
private DataSource dataSource;
public ProductDAO(DataSource dataSource) {
this.dataSource = dataSource;
}
public List<Product> list() throws SQLException {
List<Product> products = new ArrayList<Product>();
try (
Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT id, name, description, price FROM product");
ResultSet resultSet = statement.executeQuery();
) {
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getLong("id"));
product.setName(resultSet.getString("name"));
product.setDescription(resultSet.getString("description"));
product.setPrice(resultSet.getBigDecimal("price"));
products.add(product);
}
}
return products;
}
}
A servlet class which obtains the list and puts it in the request scope.
#WebServlet("/products")
public class ProductsServlet extends HttpServlet {
#Resource(name="jdbc/YourDB") // For Tomcat, define as <Resource> in context.xml and declare as <resource-ref> in web.xml.
private DataSource dataSource;
private ProductDAO productDAO;
#Override
public void init() {
productDAO = new ProductDAO(dataSource);
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<Product> products = productDAO.list();
request.setAttribute("products", products); // Will be available as ${products} in JSP
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
} catch (SQLException e) {
throw new ServletException("Cannot obtain products from DB", e);
}
}
}
Finally a JSP file in /WEB-INF/products.jsp which uses JSTL <c:forEach> to iterate over List<Product> which is made available in EL by ${products}, and uses JSTL <c:out> to escape string properties in order to avoid XSS holes when it concerns user-controlled input.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/format" prefix="fmt" %>
...
<table>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.id}</td>
<td><c:out value="${product.name}" /></td>
<td><c:out value="${product.description}" /></td>
<td><fmt:formatNumber value="${product.price}" type="currency" currencyCode="USD" /></td>
</tr>
</c:forEach>
</table>
To get it to work, just call the servlet by its URL. Provided that the servlet is annotated #WebServlet("/products") or mapped in web.xml with <url-pattern>/products</url-pattern>, then you can call it by http://example.com/contextname/products
See also:
How to avoid Java code in JSP files?
doGet and doPost in Servlets
How should I connect to JDBC database / datasource in a servlet based application?
Design Patterns web based applications
RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
How to map a ResultSet with unknown amount of columns to a List and display it in a HTML table?
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
MVC, in a web application context, doesn't consist in using a class from a JSP. It consists in using the following model :
browser sends a request to a web server
the web server is configured so that the request is handled by a servlet or a filter (the controller : Java code, not JSP code)
The servlet/filter usually dispatches the request to a specific class (called an Action, the specific part of the controller), based on configuration/annotations
The action executes the business logic (i.e. fetch the data from the database in your example : the model)
The action forwards the request to a JSP. The role of the JSP is only to generate HTML code (i.e. display your data : the view)
Since the JSP usually uses JSP tags (the JSTL, for example) and the JSP expression language, and since JSP tags and the EL are designed to get information from JavaBeans, you'd better have your data available in the form of JavaBeans or collections of JavaBeans.
The role of the controller (the action class) is thus to fetch the data, to create JavaBean instances containing the data, in a suitable format for the JSP, to put them in request attributes, and then to dispatch to the JSP. The JSP will then iterate through the JavaBean instances and display what they contain.
You should not implement the MVC framework yourself. Use existing ones (Stripes, Struts, etc.)
I don't know how should I return the ResultSet from the class file to the JSP page
Well, you don't.
The point of MVC is to separate your model ( the M DB info in this case ) from your view ( V a jsp, in this case ) in such a way you can change the view without braking to application.
To do this you might use an intermediate object to represent your data ( usually called DTO - after Data Transfer Object -, don't know how they call it these days ), and other object to fetch it ( usually a DAO ).
So basically you have your JSP file, get the request parameters, and then invoke a method from the DAO. The dao, internally has the means to connect to the db and fetch the data and builds a collections of DTO's which are returned to the JSP for rendering.
Something like this extremely simplified ( and insecure ) code:
Employee.java
class Employee {
String name;
int emplid;
}
EmployeeDAO.java
class EmployeeDAO {
... method to connect
etc.
List<Employee> getAllNamed( String name ) {
String query = "SELECT name, emplid FROM employee where name like ?";
ResultSet rs = preparedStatement.executeQuery etc etc.
List<Employee> results = ....
while( rs.hasNext() ) {
results.add( new Employee( rs.getString("name"), rs.getInt("emplid")));
}
// close resources etc
return results;
}
}
employee.jsp
<%
request.setAttribute("employees", dao.getAllNamed( request.getParameter("name") );
%>
<table>
<c:forEach items="${employees}" var="employee">
<tr><td>${employee.emplid}</td><td>${employee.name}</td></tr>
</c:forEach>
</table>
I hope this give you a better idea.
I have a problem. I don't understand clearly the code. I have a similar problem with my code.
I have created database SQL and filled up. Then I want to implement a MainServlet (code below) that richieve data from database and in a different jsp page, I want to insert that data in section like h1, h2 ecc... I must use the ${} sintax but I don't know how do that.
Briefly, In jsp file (code below, I MUST USE ${} SINTAX) I want to "call" MainServlet and there I want to richieve data from database and view in jsp file.
I hope I have explained correctly, thank you very much!
MainServlet.java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MainServlet
*/
#WebServlet({ "/MainServlet" })
public class MainServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String PATH_JSP = "/WEB-INF/";
/**
* #see HttpServlet#HttpServlet()
*/
public MainServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* #see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String doveAndare = request.getParameter("azione");
if(doveAndare==null)
doveAndare = "index";
try {
String driverString = "com.mysql.cj.jdbc.Driver";
Class.forName(driverString);
String connString = "jdbc:mysql://localhost:3306/ldd_jewels?user=root&password=";
Connection conn = DriverManager.getConnection(connString);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM JEWEL");
while (rs.next() == true) {
System.out.println(rs.getString("Category") + "\t" + rs.getString("Name"));
/* I try that but does not work
request.setAttribute("name", rs.getString("Name"));
javax.servlet.RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/widering_male.jsp");
dispatcher.forward(request, response); */
}
stmt.close();
conn.close();
} catch(Exception e) {
e.printStackTrace();
}
request.getRequestDispatcher(PATH_JSP+doveAndare+".jsp").forward(request, response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
doublerow.jsp
<section id="portfolio-details" class="portfolio-details">
<div class="container">
<div class="row gy-4">
<div class="col-lg-8">
<div class="portfolio-details-slider swiper">
<div class="swiper-wrapper align-items-center">
<div class="swiper-slide">
<img src="assets/img/jewels/doublerow_1.jpg" alt="" />
</div>
<div class="swiper-slide">
<img src="assets/img/jewels/doublerow_2.jpg" alt="" />
</div>
<div class="swiper-slide">
<img src="assets/img/jewels/doublerow_3.jpg" alt="" />
</div>
</div>
<div class="swiper-pagination"></div>
</div>
</div>
<div class="col-lg-4">
<div class="portfolio-info">
<h3>Product details</h3>
<ul>
<li><strong>Code</strong>: 1S3D5</li>
<li><strong>Category</strong>: Bracelets</li>
<li><strong>Name</strong>: Double Row Hinged Bangle</li>
<li><strong>Gender</strong>: Female</li>
<li><strong>Material</strong>: Yellow gold</li>
<li><strong>Size</strong>: 121mm</li>
<li><strong>Price</strong>: €5500</li>
</ul>
</div>
<div class="portfolio-description">
<h2>Description of product</h2>
<p>
The entwined ends of Tiffany Knot’s signature motif symbolize
the power of connections between people. Balancing strength
and elegance, each Tiffany Knot design is a complex feat of
craftsmanship. This bangle is crafted with yellow gold and
polished by hand for high shine. Wear on its own or partnered
with classic silhouettes for an unexpected pairing.
</p>
</div>
</div>
</div>
</div>
</section>
This is my database:
I want to insert each jewel in different pages (each jewel have a jsp file)
You can use the <c:forEach > tag
you can find a detailed example in the following link example use
I think it will be better for you to contain the data of the table into a collection such as list and return the list from the Java class and reuse this collection in the JSP.

Trying to pass a list from servlet to jsp but prints null

I am trying to pass an ArrayList of objects from a servlet to a jsp file but when I try printing it, it prints nothing. I used the exact lines from a similar post for the jsp...can someone help me because I have never used a jsp before.
The idea is to to traverse through an xml file using dom parser and then print its elements to an html table of a specific form. My java code successfully collects all the elements and stores them in a list which I want to pass in the jsp to format in the table asked...
SERVLET CODE (with missing pieces cause it's huge) :
import all the needed libraries
public class MyServlet extends HttpServlet {
private static xml_obj obj = null;
public static ArrayList<xml_obj> objList = new ArrayList<xml_obj>();
public static void main(String[] args){
try {
Start(); //starting the methods for the xml traversal and creates the list
//System.out.println("AA"+objList.get(1).getName());
}
catch(Exception e) {
e.getMessage();
}
}
public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ArrayList list = getList();
//System.out.println(objList.get(1).getName());
request.setAttribute ("Xml_objList", objList );
RequestDispatcher view = request.getRequestDispatcher("DomNav.jsp");
view.forward (request,response);
}
static void Start(){
/*..........code missing.............*/
myDOMTreeProc dtp = new myDOMTreeProc();
dtp.processLvl(ListOfCh, 0); //processLvl -> method in myDOMTreeProc
}
public static ArrayList<xml_obj> getList() {
return objList;
}
}
class myDOMTreeProc {
/*........DOM XML TRAVERSE.......*/
}
class attribute {
private String Name;
private String Value;
/*.............setters/getters.......*/
}
class xml_obj {
public int Lvl;
private String Name;
private String Value;
private String Parent;
private ArrayList<attribute> attributes=null;
/*.............setters/getters.......*/
}
JSP 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"%>
<%#page import="java.util.*" %>
<!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>Expression Language Example</title>
</head>
<body>
<h1>TEST JSP</h1>
<% ArrayList list = (ArrayList) request.getAttribute("Xml_objList"); %>
<c:forEach var="item" items="${Xml_objList}">
${item.Lvl}
</c:forEach>
</body>
</html>
The list is correct I tested it. I think the problem is when I pass it in the jsp.
Follow Java Naming convention and everything will work fine. Just replace int Lvl with int lvl;
JSP:
<c:forEach var="item" items="${Xml_objList}">
${item.lvl}
</c:forEach>
Instead of ${item.lvl} you can try with ${item.getLvl()} or ${item['lvl']}

Model objects are not passed to jsps in Spring MVC - InternalResourceView - renderMergedOutputModel

I am using JstlView Spring Tiles in my project.
When i try to send form:errors from validator to jsp through controller,errors are not being dispayed in jsp
when i debug,
1)errors are being printed in controller class before returning modelandview , from validator class.
2)Then error are also being printed in JstlView class from controller class.
So i think, requestDispatcher.forward(request, response) in jstlview class is not returning model data to jsp.
Because when i tried HttpServletRequest or HttpServletResponse objects to retrive errors in jsp , values are being dispalyed in jsp.
Can some one help me in this used.
How can i send model data to jsp renderMergedOutputModel- RequestDispatcher , redirect method.
My code is as follows,
JstlView class:
public class JstlView extends InternalResourceView {
#Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(request, response);
// set original view being asked for as a request parameter
request.setAttribute("partial", dispatcherPath.substring(dispatcherPath.lastIndexOf("/") + 1));
// force everything to be template.jsp
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/template.jsp");
requestDispatcher.forward(request, response);
System.out.println("**********************"+model.get("userName"));
}
}
Servlet.xml
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="com.tms.web.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Controller:
private LoginValidator loginValidator;
#Autowired
public void setUserValidator(LoginValidator loginValidator) {
this.loginValidator = loginValidator;
}
final RequestHandler requestHandler = new RequestHandler();
#RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() {
return new ModelAndView("login", "users", new users());
}
#RequestMapping(value = "/logins",method = RequestMethod.POST)
public ModelAndView validateUser(#Valid users user, BindingResult result,#ModelAttribute("users")users users,ModelMap model,RedirectAttributes redirectAttributes,HttpServletRequest req)
{
this.loginValidator.validate(user, result);
if (result.hasErrors())
{
model.putAll(result.getModel());//values not retuned to jsp
req.setAttribute("userName", result.getFieldError().getDefaultMessage().toString()); //working fine values retuned to jsp
return new ModelAndView("/login", model);
}
else
{
//succes related code
return new ModelAndView(redirect, model);
}
}
Validator
#Component
public class LoginValidator implements Validator {
#Override
public boolean supports(Class<?> clazz) {
return clazz.isAssignableFrom(users.class);
}
#Override
public void validate(Object obj, Errors errors) {
users user = (users) obj;
String userName = user.getUserName();
String password = user.getPassword();
validateuserName(userName,password, errors);
}
private void validateuserName(String userName, String password,Errors errors) {
if (!(isValidString(userName))) {
errors.rejectValue("userName", "userName.required","Username should not be blank");
}
else if(!(isValidString(password)))
{
errors.rejectValue("password", "password.required","Password should not be blank");
}
}
private boolean isValidString(String str) {
return isNotNull(str) && (str.length() > 0 ) && !(str.isEmpty());
}
private boolean isNotNull(String str) {
return str != null;
}
}
Jsp
<%# page language="java" contentType="text/html;charset=UTF-8; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%# page isScriptingEnabled="true" isELIgnored="false" %>
<!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"%>
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<form:form method="POST" action="/TMSWeb/logins" commandName="users" modelAttribute="users">
<input name="password" type="password" align="center"/>
<form:errors path="password" cssClass="error" element="div" />
<input type="submit" class="buttonorange" value="Login"/>
</form:form>
In Spring MVC when I can access my beans in JSP without JstlView's, then, in my JSP I can write (${errors). But when the same JSP is a part of a tiles view, these properties aren't accessible.
Can someone help me in this issue?
You are extending the class org.springframework.web.servlet.view.InternalResourceView in your JstlView class and override renderMergedOutputModel method, where you are missing to expose the model object. call this method exposeModelAsRequestAttributes(model, requestToExpose); from override method as shown below. Try with below code, which is worked for me.
#Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Expose the model object as request attributes.
exposeModelAsRequestAttributes(model,request);
// Determine the path for the request dispatcher.
String dispatcherPath = prepareForRendering(request, response);
// set original view being asked for as a request parameter
request.setAttribute("partial", dispatcherPath.substring(dispatcherPath.lastIndexOf("/") + 1));
// force everything to be template.jsp
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/jsp/template.jsp");
requestDispatcher.forward(request, response);
System.out.println("**********************"+model.get("userName"));
}
}

JSP EL and scope attribute confusion

I would like to ask you some help in clarifying a few issues. But, before anything, some code is inbound first - it's a really simple login example I've constructed.
Container is Tomcat 5.5.27.
Let's assume correct username and pass combination is entered; questions are at the bottom.
LoginPage.jsp (entrypoint - view)
<%# page language="java" contentType="text/html; charset=windows-1250" pageEncoding="windows-1250"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<div id="page">
<div id="content_container">
<div id="content">
<form action="LoginServlet">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit" value="Submit">
</form>
</div>
</div>
</div>
</body>
</html>
LoginServlet.java (controller)
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
UserBean user = new UserBean();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user = UserDAO.login(user);
if(user.isValid()){
HttpSession session = request.getSession();
session.setAttribute("currentSessionUser", user);
response.sendRedirect("userLogged.jsp");
} else {
response.sendRedirect("invalidLogin.jsp");
}
} catch (Exception e){
e.printStackTrace();
}
}
}
UserDAO.java ("service" class)
//snipped imports and such
public class UserDAO {
static Connection currConn = null;
static ResultSet rs = null;
public static UserBean login(UserBean userBean) {
Statement stmt = null;
String username = userBean.getUsername();
String password = userBean.getPassword();
String searchQuery = "SELECT * FROM pilots x WHERE x.email = '" + username + "' AND x.password = '" + password + "'";
System.out.println("Your user name is " + username);
System.out.println("Your password is " + password);
System.out.println("Query: " + searchQuery);
try {
currConn = ConnectionManager.getConnection();
stmt = currConn.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean more = rs.next();
if (!more) {
System.out.println("Sorry, you are not a registered user! Please sign up first");
userBean.setValid(false);
} else {
String firstName = rs.getString("FIRST_NAME");
String lastName = rs.getString("LAST_NAME");
System.out.println("Welcome " + firstName);
userBean.setFirstName(firstName);
userBean.setLastName(lastName);
userBean.setValid(true);
}
} catch (Exception ex) {
System.out.println("Log In failed: An Exception has occurred! " + ex);
ex.printStackTrace();
} finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(currConn != null){
try {
currConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return userBean;
}
}
UserBean.java (model, a classic POJO/bean used as a DTO)
//...
public class UserBean {
private String username;
private String password;
private String firstName;
private String lastName;
private boolean valid;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
}
userLogged.jsp (exitpoint - view) --never mind the div elements-
<%# page language="java" contentType="text/html; charset=windows-1250" pageEncoding="windows-1250"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Successful login!</title>
</head>
<body>
<div id="page">
<div id="content_container">
<div id="content">
<jsp:useBean id="currentSessionUser" class="examplePackage.UserBean" scope="application">
Welcome, <jsp:getProperty name="currentSessionUser" property="username"/> <br>
********<br>
Test 0 -> ${param.name}<br>
Test 1 -> ${paramValues.name[0]}<br>
Test 2 -> ${paramValues[name[0]]}<br>
Test 3 -> ${param["name"]}<br>
Test 4 -> ${param.username}<br>
Test 5 -> ${param["username"]}<br>
Test 6 -> ${sessionScope.currentSessionUser.username}<br>
*******<br>
Test 7 -> ${header.host}<br>
Test 8 -> ${header["host"]}<br>
Test 9 -> ${pageContext.request.method}<br>
</jsp:useBean>
</div>
</div>
</div>
</body>
</html>
Webpage output is as follows (c/p directly from FireFox):
Welcome, USER_X
********
Test 0 ->
Test 1 ->
Test 2 ->
Test 3 ->
Test 4 ->
Test 5 ->
Test 6 -> USER_X
*******
Test 7 -> localhost:8080
Test 8 -> localhost:8080
Test 9 -> GET
1) My first question is regarding the scope - which scope is actually applicable?? If you checkout userLogged.jsp, lines 13 and 22 (L13 and L22), you'll see my dilemma - if I use any other scope than "application" in L13, L14 returns null value. On the other hand, should I use applicationScope on L22, it returns null (as it darn well should, since I am setting a SESSION attribute, not a context attribute!).
So, the question is - why should I use application scope on L13 anyway?? I'd expect nothing other than session scope, as can be seen from my controller.
2) The other question is regarding EL - why can't I fetch request parameters in Tests 0-5? Other stuff works fine (as can be seen from output), but I can't understand how to make these request parameters printed out as I inteded (via request EL implicit objects).
3) I am also curious as to why this won't work if I were to use it (L24 of userLogged.jsp, change attribute to property="*")?
Welcome, <jsp:getProperty name="currentSessionUser" property="*"/>
It returns null, and I've matched my domain object (UserBean) properties according to JavaBeans spec. I'd expect it would return ALL userBean properties that are matchable to input type field from LoginPage.jsp and are of correct type to use the feature (must be String or primitive).
Thank you very much in advance
With regards
EK
You do not need jsp:useBean or jsp:getProperty. Get rid of them. You're already using servlets and you have already put the logged in user in the session scope with the key currentSessionUser in this line:
session.setAttribute("currentSessionUser", user);
All you need to do to display the username is the following:
<p>Welcome, ${currentSessionUser.username}</p>
To prevent XSS, use JSTL c:out:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<p>Welcome, <c:out value="${currentSessionUser.username}" /></p>
You definitely don't want to put it in the application scope. It'll be applied on all website visitors.
As to your request parameter question: you're firing a redirect using response.sendRedirect(). This will basically instruct the webbrowser to create a brand new request on the given URL. You didn't pass any parameters along it, so it will indeed not be available in the redirected request. It is all working as expected. If you want to have the original request parameters still available in the result page, then you should either forward the request using RequestDispatcher#forward()
request.getRequestDispatcher("page.jsp").forward(request.response);
or pass the parameters along the redirect
response.sendRedirect("page.jsp?param1=" + URLEncoder.encode(param1, "UTF-8"));
By the way, there is a major problem in your DAO code: it is not threadsafe. The connection and resultset are been declared static. It is also prone to resource leaks, the closing isn't been taken place in finally.
See also:
Beginning and intermediate JSP/Servlet tutorials
Hidden features of JSP/Servlet
Update as per the comments:
When you reference an object in EL context using ${key}, it will under the hoods use JspContext#findAttribute() to locate the associated value in respectively the page, request, session and application scopes and return the first non-null value.
As to the jsp:useBean, you're basically defining a new bean in the application scope instead of referencing the existing bean in the session scope. If you reference it explicitly in the application scope as ${applicationScope.currentSessionUser} you'll see that it doesn't return the same as you've set in the session scope in the servlet. You need to replace scope="application" by scope="session".
As to the property="*", this only works when you forwards the request as answered before. Those will namely be set from request parameters.
And no, finally is certainly not an anti-pattern. It's however one of the most misunderstood/undervalued keywords among beginners. The finally block doesn't make it threadsafe. It prevents resource leaking. Removing static and declaring the resources in method local block will make it threadsafe. Regarding the DAO pattern, you may find this article useful.
If you use jsp:useBean with the class attribute, a new bean will be instantiated and put in the requested scope. To reuse a bean taht is already available in some scope you would have to use the type attribute and set scope to "session" for example. The relationships between the different attributes are described at http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html.

Is a custom JSF converter needed for this simple class?

I have been trying with limited success to code a JSF application. In one section of the application, I need users to select from a select menu which displays a list of selectable status values. The Status class (presented below), which is used to populate the List that is displayed in the select menu, is a simple class made up of two Strings: one is the code used to look up the description in the database, the other is the human-readable description. I am trying to find out if I need a converter here at all, and if so, how best to implement the converter. This is a JSF 1.1 project using Java 1.5
I am using the following code in the JSP:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
<html>
<h:graphicImage id="image" url="/images/appname.jpg"
alt="app name" />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<jsp:include page="/jsp/menu.jsp" />
</head>
<body>
<h:outputText
value="Add Value"
style="font-size:20px;" />
<h:messages errorStyle="color: red" infoStyle="color: green"
layout="table" />
<h:form id="statusReasonEditForm">
<table>
<tr>
<td><h:outputText id="txtvalue" value="Status" /></td>
<td><h:selectOneMenu id="selectStatus"
value="#{pc_statusReasonBacker.status}"
binding="#{pc_statusReasonBacker.selectItem}">
<f:selectItems value="#{pc_statusReasonBacker.selectStatuses}" />
<f:converter converterId="statusConverter" />
</h:selectOneMenu>
<td><h:outputText id="txtvaluereason" value="Status Reason" /></td>
<td><h:inputText id="txtinputreason"
value="#{pc_statusReasonBacker.statusReason.statusReason}"
maxlength="100" size="40" /></td>
<td><h:outputText id="txtvaluereasondesc"
value="Status Reason Desc" /></td>
<td><h:inputText id="txtinputreasondesc"
value="#{pc_statusReasonBacker.statusReason.statusReasonDesc}"
maxlength="100" size="40" /></td>
</tr>
</table>
<tr>
<td><h:commandButton id="savebutton" value="Save"
action="#{pc_statusReasonBacker.save}" /></td>
<td><h:commandButton id="cancelbutton" value="Cancel"
action="#{pc_statusReasonBacker.cancel}" /></td>
</tr>
</h:form>
<hr />
</body>
</html>
</f:view>
The backing bean is shown here (some non-related sections, such as paging, removed for clarity):
public class StatusReasonBacker {
private List<StatusReason> statusReasonList;
private List<Status> statusList;
private List<SelectItem> selectStatuses;
private StatusReason statusReason;
private StatusDao sDao;
private Status status;
private UIData statusReasonTable;
private HtmlSelectOneMenu selectItem;
private String selectedStatus = "";
public StatusReasonBacker() {
sDao = new StatusDao();
statusReason = new StatusReason();
selectStatuses = new ArrayList<SelectItem>();
status = new Status();
selectItem = new HtmlSelectOneMenu();
}
public String insert() {
status.setStatusCde("");
statusReason.setStatus(status);
statusReason.setStatusReason("");
statusReason.setStatusReasonCde("");
statusReason.setStatusReasonDesc("");
return "success";
}
public String edit() {
this.statusReason = (StatusReason) statusReasonTable.getRowData();
selectItem.setValue(statusReason.getStatus().getStatusCde());
return "success";
}
public String update() {
if ("".equalsIgnoreCase(statusReason.getStatusReason().trim())) {
Message
.addErrorMessage("You must enter a value for the status reason.");
return "failure";
} else if (("".equalsIgnoreCase(statusReason.getStatusReasonDesc()
.trim()))) {
Message
.addErrorMessage("You must enter a value for the status reason description.");
return "failure";
}
sDao.updateStatusReason(statusReason);
return "statusreasons";
}
public String delete() {
StatusReason statReason = (StatusReason) statusReasonTable.getRowData();
sDao.deleteStatusReason(statReason);
return "statusreasons";
}
public String cancel() {
return "statusreasons";
}
public String save() {
statusReason.setStatus(status);
sDao.insertStatusReason(statusReason);
return "statusreasons";
}
...
public StatusDao getSDao() {
return sDao;
}
public void setSDao(StatusDao dao) {
sDao = dao;
}
public List<StatusReason> getStatusReasonList() {
statusReasonList = sDao.getStatusReasons();
return statusReasonList;
}
public void setStatusReasonList(List<StatusReason> statusReasonList) {
this.statusReasonList = statusReasonList;
}
public UIData getStatusReasonTable() {
return statusReasonTable;
}
public void setStatusReasonTable(UIData statusReasonTable) {
this.statusReasonTable = statusReasonTable;
}
public StatusReason getStatusReason() {
return statusReason;
}
public void setStatusReason(StatusReason statusReason) {
this.statusReason = statusReason;
}
public List<Status> getStatusList() {
statusList = sDao.getStatuses();
return statusList;
}
public void setStatusList(List<Status> statusList) {
this.statusList = statusList;
}
public List<SelectItem> getSelectStatuses() {
selectStatuses.clear();
if (statusList == null) {
statusList = this.getStatusList();
}
for (Status sr : statusList) {
SelectItem si = new SelectItem();
si.setValue(sr.getStatusCde());
si.setLabel(sr.toString());
si.setDescription(sr.toString());
selectStatuses.add(si);
}
return selectStatuses;
}
public void setSelectStatuses(List<SelectItem> selectStatuses) {
this.selectStatuses = selectStatuses;
}
public String getSelectedStatus() {
selectedStatus = statusReason.getStatusDesc();
return selectedStatus;
}
public void setSelectedStatus(String selectedStatus) {
this.selectedStatus = selectedStatus;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public HtmlSelectOneMenu getSelectItem() {
return selectItem;
}
public void setSelectItem(HtmlSelectOneMenu selectItem) {
this.selectItem = selectItem;
}
}
Thanks!
I am trying to find out if I need a converter here at all, and if so, how best to implement the converter.
You need a converter whenever you want to pass non-standard Java Objects from a HTTP request to another HTTP request. With non-standard I mean not a String, Number or Boolean. This all simply because HTTP request parameters can only be Strings. That Number and Boolean works is because EL can recognize them and has built-in coercions for it.
For non-standard Java Objects you need to implement a javax.faces.convert.Converter which converts the Object to a String (or a Number so you want, for example a Long id which can be the PK of the associated row in database table) inside the getAsString() method before displaying in HTML. You do the other way round in the getAsObject() method during processing of the request parameters (e.g. get the associated object from DAO by its id).
You can find here an example of how to use a Converter for a h:selectOneMenu. You see that this article also contains an alternative, but you'll need to do a bit more work in the backing bean to convert (map) the objects yourself.

Categories

Resources