jsp page not recognizing java methods - java

I am currently working on a project that implies passing a aspx.net project to jsp.
The issue that I have been working on all day is as follows:
I create the following methods in a java file named DefaultMethods:
package model;
import com.merlin.MXL;
public class DefaultMethods {
public DefaultMethods() {
super();
}
public String versionNumber(){
try{
MXL mxl = new MXL();
return mxl.merlinVersion();
}
catch (Exception ex){
return "Contact MerlinXL";
}
}
public String serverName(){
try{
MXL mxl = new MXL();
return mxl.serverName();
}
catch (Exception ex){
return "Contact MerlinXL";
}
}
}
Where MXL is the webservice specifically created for this application.
So then I go to the jsp page and import the java class and call the methods in <% %> tags:
<%# page import="model.DefaultMethods"%>
<%# page language="java" contentType="text/html; charset=iso-8859-1" pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MerlinXL</title>
<link href="web/css/merlinxl.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="/favicon.ico" />
<style type="text/css">
Now go a series of css styles, tables, etc. And then I call the methods here:
<tr>
<td valign="top" class="style2">
<strong>
<%
DefaultMethods defaultMethods = new DefaultMethods();
String theNumber = defaultMethods.versionNumber();
%>
<label ID="labVersion" style="font-weight: 700; font-size: small; text-align: center"/>
Version <%= theNumber %>
</label>
</strong>
</td>
<td valign="top" class="auto-style1">
<p style="margin-left: 0px">
</p>
</td>
<td valign="top" class="style3">
<strong>
<%
DefaultMethods defaultMethods2 = new DefaultMethods();
String theServer = defaultMethods2.serverName();
%>
<label ID="labServer" style="font-weight: 700; font-size: small; text-align: center"/>
Server Name <%= theServer %>
</label>
</strong>
</td>
</tr>
Now, the funny thing is that the imports work fine, gives no mistakes on the client side when loading the page on the server, what's more, when I press Ctrl + Space in the IDE (JDeveloper) on the instance of DefaultMethods it autompletes giving me all the methods I have declared in the class as options. But when it loads on the server, it keeps giving me error coming up as method not found
I know I'm not coding in the best practice yet, I wanted to start a simple one to see if it worked and then go in with MVC.
Yes, I have googled, and yes, I have looked up the solutions in here, but I seem to have everything in order with my code and I'm out of ideas or just can't see the tree from the woods.

Well, needless to answer the comments, as the code worked beautifully after I opened the project up this morning.
There has to be some kind of bug in JDeveloper when creating new packages and files, odd thing is I restarted the web service to make sure that the new folders would integrate, but by the looks of it JDeveloper needs, at the very least, to be restarted in order to integrate new folders/files.
Definitely this IDE is a great let down from Oracle.
Thank you all for your comments.

Related

Using a form to display model objects in Thymeleaf. Is there a better way to do this?

been trying to get comfortable with Spring Boot/Thymeleaf/frontend development in general. I'm currently using a table on the frontend to display a list of objects from the backend, as well as a form to allow users to view properties of each item in the list.
I feel like the way I'm doing this is really weird and feel that there could be a better way. Would appreciate any tips/feedback, thank you.
HTML/Thymeleaf
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Gambler Card List</title>
<link th:href="#{/css/styles.css}" rel="stylesheet" />
</head>
<body>
<h1>Gambler Card List</h1>
<div class="cardlist-wrapper">
<div class="cardlist-names">
<form action="/gambler/cardlist" method="get">
<table class="cardlist-table">
<tr th:each="card : ${cardList.getCardList()}">
<td>
<input th:class="card-btn"
th:attr="id=${{card.getId()} == {currentCard.getId()} ? 'selected' : ''}"
type="submit" th:value="${card.getName()}" name="cardName">
</td>
</tr>
</table>
</form>
</div>
<div class="cardlist-description">
<h4 th:text="${currentCard.getDescriptionTitle()}"></h4>
<p th:text="${currentCard.getDescription()}"></p>
</div>
</div>
</body>
</html>
Controller
#RequestMapping(value = "gambler/cardlist")
public String baseCardList(Model model,
#RequestParam(value="cardName", required=false) String cardName) {
model.addAttribute("currentCard", cardList.getCardByName(cardName));
model.addAttribute("cardList", cardList);
return "gambler/cardlist";
}
Output (need 10 rep to post image, so here's a link)
One potential improvement/simplification is to remove the form and replace the inputs with anchors. The anchors would look something like this (not tested!):
<a th:href="#{/gambler/cardlist(cardName=${card.name})}">
<button th:text="${card.name}"
th:classappend="${card.id == currentCard.id ? 'selectedButton' : 'deselectedButton'}">Card Name</button>
</a>
You can style selectedButton and deselectedButton as you wish.

Loading Image From Server using path [duplicate]

This question already has answers here:
"Not allowed to load local resource: file:///C:....jpg" Java EE Tomcat
(8 answers)
Closed 2 years ago.
I am trying to create a simple application in which a user can add an image. The name of the image and the path of the folder where I am storing the image but when trying to retrieve it. It shows "Not allowed to load local resource: file:///H:/Projectpictures/BrowserForDataTables.PNG" Error on browser console. please have a look and let me know what is it I am doing wrong here.
Here is my code.
This is the DAOImplementation code for retrive:
#Override
#Transactional
public List<pictureModal> allImages() {
Session currentSession = sessionFactory.getCurrentSession();
Query<pictureModal> query = currentSession.createQuery("from pictureModal");
List<pictureModal> pictues= query.getResultList();
return pictures;
}
This is Controller code for getimages:
#RequestMapping("/seeAllImages")
public String getAllImage(Model theModal) {
List<pictureModal> myPictures= pictureServ.allImages();
theModal.addAttribute("myPicture",myPictures);
return "ImagePage";
}
And here is the code for JSP file:
<%# 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"%>
<html>
<head>
<title>Pictures will display here</title>
</head>
<body>
<h2>Display Pictures </h2>
<table border="1">
<thead>
<tr>
<th>Picture Name</th>
<th>Picture</th>
</tr>
</thead>
<c:forEach var="tempPic" items="${myPicture}">
<tbody>
<tr>
<td>${tempPic.name}</td>
<td><img src="${tempPic.path}"></td>
</tr>
</tbody>
</c:forEach>
</table>
</body>
</html>
Maybe your program tries to access a location where does not have the correct rights. Maybe that is an administrator folder, and your program isn't started as an Administrator.

Spring MVC having trouble getting JSP to display Controller Value

I am new to Spring MVC I have setup a test DB to try to get a single flow through and have a table displayed in a JSP page. I assume I have something configured wrong but I cannot find it. Maybe a naming convention.
I have a mySql DB
A DAO Class that just does a select * from my table that works as I can step into it.
My controller just gets a list of my Run Models. No errors are thrown The JSP simple loads an empty table. The controller seems to have the correct info in it.
Model (snippet all the getters and setters seem to work as the controller gets a list of them fine)
public class QAModel {
private int idRun;
private String SuiteID;
private String run_name;
Controller Code
#RequestMapping(value="/RunList")
public ModelAndView listRun(ModelAndView model) throws IOException{
//#ModelAttribute
System.out.println("**** Controller ******");
List<QAModel> listRun = runDao.list();
model.addObject("RunList", listRun);
model.setViewName("RunList");
return model;
}
That does get a list of Run Model Objects that contain the correct DB info I can see them if I step into it. However the JSP loads a blank table.
JSP Code (I assume I'm missing some mapping or naming convention?)
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Contact Manager Home</title>
</head>
<body>
<div align="center">
<h1>Contact List</h1>
<h3>New Run</h3>
<table border="1">
<th>No</th>
<th>Suite ID</th>
<th>Run Name</th>
<c:forEach var="QAModel" items="${RunList}" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${QAModel.SuiteID}</td>
<td>${QAModel.run_name}</td>
<td>
Edit
Delete
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
If you are not able to print any jstl variable then one possible reason can be that your jstl core tag is not added.
Add this to begining of your JSP file.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

How to convert this JSP to an MVC approach? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I've written the below JSP. Now I want to make it into a MVC pattern, could you please help me how to do it?
<%#page import="java.util.Date"%>
<%#include file="DBCon.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>Insert title here</title>
<script language="javascript">
function UnBloc1(test){
var temp3id= 'temp3' + test;
var temp4id= 'temp4' + test;
//alert(temp3id);
document.getElementById(temp3id).style.display='block';
document.getElementById(temp4id).style.display='block';
}
function invoke(but1)
{
//var x=document.getElementById("temp3"+but).value;
//alert(x);
document.abc.action="Up_Query_DB.jsp?val1="+but1;
document.abc.submit();
}
function invoke1(but)
{
document.abc.action="Users_2.jsp?val="+but;
document.abc.submit();
//var t=document.getElementById("ab")+z;
//alert(t);
}
</script>
</head>
<body>
<form name="abc" method="post" action=""><table>
<%
try
{
String s=(String)session.getAttribute("muusername");
int i=0;
int temp=0, temp1=0,temp2=0, temp3=0, temp4=0;
ps=con.prepareStatement("Select DBID,Query_Raised,TR from Scope2 where TR!='null' AND (Query_Answered is null OR Count1 is null) And Specialist='"+s+"'");
rs=ps.executeQuery();
out.println("<b>QueryRaised</b>");
while(rs.next())
{
i++;
%>
<tr>
<td><input type="text" value="<%=i%>" name="id1" id="id1"></td>
<td><center><input type="text" value="<%=rs.getString("DBID")%>" readonly id="abc<%=i%>" name="abc<%=i%>" size="100"></center></td>
<td><input type="Submit" value="Resume" name="temp1<%=i%>" id="temp1<%=i%>" onclick="invoke1(<%=i%>)"></td>
<td><input type="button" value="Update Answer" name="temp2<%=i%>" id="temp2<%=i%>" onClick="UnBloc1(<%=i%>)"></td>
<td><input type="text" name="temp3<%=i%>" id="temp3<%=i%>" style="display: none"/></td>
<td><input type="Submit" value="Submit Answer" name="temp4<%=i%>" id="temp4<%=i%>" style="display: none" onClick="invoke(<%=i%>)"/> </td>
</tr>
<% }
}
catch(Exception e)
{
out.println(e);
}
%>
</table>
</form>
</body>
</html>
Let's say you go to the database and get a list of objects that contain few attributes (with setter and getter methods), among them, the dbid attribute. The object would be your model. Let's call it myBean.
The Java class from you will do the query will be the Controller class. Let's call it myController. So, the controller should: 1. Get the list of objects you need. 2. Do anything that should be done before representing the information (in your case, I would say nothing) and 3. pass the information to the JSP through the list of your beans setting the information in the request so the information can be displayed.
Your JSP, now, should look like this:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# 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>
<script language="javascript">
...
</script>
</head>
<body>
<form name="abc" method="post" action="">
<table>
<c:forEach var="myItem" items="${listDbid}" varStatus="i">
<tr>
<td><input type="text" value="${i.count}" name="id1" id="id1"></td>
<td><center><input type="text" value="${myItem.getDbid}" readonly id="abc${i.count}" name="abc${i.count}" size="100"></center></td>
<td><input type="Submit" value="Resume" name="temp1${i.count}" id="temp1${i.count}" onclick="invoke1(${i.count})"></td>
<td><input type="button" value="Update Answer" name="temp2${i.count}" id="temp2${i.count}" onClick="UnBloc1(${i.count})"></td>
<td><input type="text" name="temp3${i.count}" id="temp3${i.count}" style="display: none"/></td>
<td><input type="Submit" value="Submit Answer" name="temp4${i.count}" id="temp4${i.count}" style="display: none" onClick="invoke(${i.count})"/> </td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
So, basically, you need to remove from your JSP all the code that is not "view", take it to a "controller" and use a "model" for sending the information from the "controller" to the "view".
Also
You should avoid scriptles (<% %>) in your JSP, better to use JSTL and others similar tools. If you do it, probably your JSP will not have "controller" code.
I recommend you to use a MVC framework, like Spring MVC or Struts, among others.
It is also nice if you create another layer in your code just for database access. This database layer will be used by the controllers so the controllers. You can reuse code, make controllers independent of database and keep them clearer.
I am new too MVC as well, but I can suggest you some tips. Make a bean. Beans are Java class with getter and setter. In your case the bean would be
public class Scope2
{
String dbID;
//all other attributes of the table.Beans should be reusable so usually there is only one bean for one corresponding table
public String getDBID()
{
return dbID;
}
public void setDBID(String dbId)
{
this.dbId=dbId;
}
//Other getters and setters for all other attributes
}
Now make a class that would perform database query. And return bean Scope2 from that class to jsp. And in your jsp you will simply print the values as out.println(bean.getDBID());

include a jsp as a header based on a condition inJSF

I'm using JSF-2.0 and I'm trying to include a jsp as a header for my current jsp.But all i want is the included jsp should be altered based on the login credentials.
More clearly...depending on the person logging in to my application, the header menu (included jsp) should be different.I've tried implementing in the below way but it did not work..any help would be appreciated
<html>
<head></head>
<body>
<%
String menuHeader = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("menuAssigned");
if (menuHeader.equals("XX")){ %>
<f:view> <jsp:include page="XHeader.jsp" /> </f:view>
<% }else if(menuHeader.equals("YY")){ %>
<f:view> <jsp:include page="YHeader.jsp" />
<%}%>
---
</f:view>
</body>
</html>
Don't use Scriptlets. Ever.
Your menuAssigned variable is just available in EL by #{menuAssigned}. I suggest to align your menuAssigned variable value with the JSP include filename. Then you can just use
<jsp:include page="#{menuAssigned}Header.jsp" />
Imagine that menuAssigned is XX, then this will include XXHeader.jsp.
Unrelated to the concrete problem, why are you using legacy JSPs while you're apparently already on JSF 2.0 which comes along with JSP's awesome successor Facelets (XHTML)?
Short answer, use EITHER JSP or JSF flow control. Don't mix them too much.
<html>
<head></head>
<body>
<f:view>
<h:panelGroup rendered="#{menuHeader == 'XX'}">
<%#include file=”XHeader.jsp" %>
</h:panelGroup>
<h:panelGroup rendered="#{menuHeader == 'YY'}">
<%#include file=”YHeader.jsp" %>
</h:panelGroup>
</f:view>
</body>
</html>
Perhaps static includes? Again, I've been using facelets with JSF for several years now. Not used to the JSP stuff anymore. Its been a while.

Categories