JSP get/setParameter with whole Objects -
im new in community , come first question.
im using jsp created jsp-sites using jsp:setparameter , jsp:getparameter single strings.
now want same whole objects (yes know string object :p). tried following:
this bean:
@entity @table(name="tbl_spj_movie") public class movie implements serializable { @id @column(name="movie_id", unique=true) private int movieid = 0; @column(name="movie_backdroppath") private string backdroppath = ""; @column(name="movie_originaltitle") private string originaltitle = ""; @column(name="movie_popularity") private double popularity = 0.0; @column(name="movie_posterpath", length=1000) private string posterpath = ""; @temporal(temporaltype.date) @column(name="movie_releasedate") private date releasedate = null; @column(name="movie_title") private string title = ""; @column(name="movie_adult") private boolean adult = false; @column(name="movie_budget") private int budget = 0; @column(name="movie_homepage", length=1000) private string homepage = ""; @lob @column(name="movie_overview", length=1000) private string overview = ""; @column(name="movie_runtime") private int runtime = 0; @column(name="movie_voteaverage") private double voteaverage = 0.0; @column(name="movie_votecount") private int votecount = 0; public movie() {} //########################################################################################## @suppresswarnings("unchecked") @transient public list<movie> getmovies() { list<movie> movies = new arraylist<movie>(); entitymanagerfactory factory = persistence.createentitymanagerfactory("spjunit"); entitymanager em = factory.createentitymanager(); query query = em.createquery("select m movie m"); movies = (list<movie>)query.getresultlist(); return movies; } @transient private movie currentmovie = null; public movie getcurrentmovie() { return currentmovie; } public void setcurrentmovie(movie currentmovie) { this.currentmovie = currentmovie; } //########################################################################################## // getter , setter public string tostring() { return title; } }
this movies.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <jsp:usebean id="movie" class="de.movie"></jsp:usebean> <!doctype html> <html> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <head> <link href="../css/movies.css" type="text/css" rel="stylesheet"> <title>movie titles</title> </head> <body> <div id="wrapper"> <br></br> <h2 id="title">movie titles</h2> <br></br> <ul> <c:foreach var="movie" items="${movie.movies}"> <li id="listelement"> <form action="../pages/singlemovie.jsp"> <input type="submit" name="currentmovie" value="${movie}" /> </form> </li> </c:foreach> </ul> </div> </body> </html>
here have big list of movies. think because of tostring() method titles printed out right? these forms want submit selected movie jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <jsp:usebean id="movie" class="de.movie"></jsp:usebean> <!doctype html> <html> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <head> <link href="../css/singlemovie.css" type="text/css" rel="stylesheet"> <title>movie</title> </head> <body> <div id="wrapper"> <jsp:setproperty name="movie" property="*" /> <br></br> <h2 id="title"><jsp:getproperty name="movie" property="currentmovie" /></h2> <br></br> </div> </body> </html>
it seems there title of movie sent because following error when call singlemovie.jsp:
org.apache.jasper.jasperexception: org.apache.jasper.jasperexception: org.apache.jasper.jasperexception: pwc6338: cannot convert "ariel" attribute currentmovie of bean de.movie: pwc6348: property editor not registered propertyeditormanager
"ariel" title of movie selected submit.
i want send whole movie can use fields of movie-bean in singlemovie.jsp.
as long worked titles of movies worked great.
so doing wrong?
you don't seem realize html , http don't work objects. can submit string parameters.
you should not build app jsps only. jsps great generate html. rest (getting parameters, validating them, retrieving , saving objects from/to database, etc.) should done using java code, in servlet, or in controller of preferred mvc framework.
you shouldn't have send whole movie server display it. movie in database, @ server-side. server needs client id of movie must display. app should work this:
- a request sent listmovies servlet
- the servlet searches movies in database, , stores list of movies in request attribute
- the servlet forwards listmovies.jsp
- the listmovies use jstl , el iterate through movies stored in request attribute, and, each of them, generates link
displaymovie?id=themovieid
. don't use form containing button when link should used. - the user clicks on link. request sent displaymovie servlet
- the servlet gets value of
id
parameter. finds movie in database using id, , stores in request attribute - the servlet forwards displaymovie.jsp page
- the displaymovie.jsp page uses jstl , el display details of movie
forget jsp:usebean, jsp:setproperty , jsp:getproperty. belong past, , shouldn't used anymore.
also, methods execute queries against database movies shouldn't in movie class. should in moviedao
class. doesn't make sense instantiate movie other movies database. , entity's responsibility not query database.
Comments
Post a Comment