Intellij IDE JSP path resolving [duplicate] - java

This question already has an answer here:
How specify path to .JSP file for request.getRequestDispatcher()?
(1 answer)
Closed 6 years ago.
I have a servelet called RegisterUser mapped as /register this servelet includes a jsp file called register.jsp . here is the structure of the project :
inside the servelet this code cannot find the jsp file :
RequestDispatcher view = request.getRequestDispatcher(registerView);
and the content of registerView is :
private static final String registerView = "../web/WEB-INF/views/Register.jsp";
It worked fine on eclipse but when I switched to Intellij it shows that the file cannot be found .

Liferay has a somewhat strange layout for it's web-inf and html folders. In Eclipse I never really code path completion to work well in JSPs because it always looked at the wrong spot for files. In IntelliJ I just setup different facets that will help the IDE help me. Here is an example. I have a facet called WEB-INF. In Liferay, when you reference a file in WEB-INF (tagfiles are a GREAT example here) you do it like this:
<%# taglib prefix="showMore" tagdir="/WEB-INF/tags/wj/showmore" %>
The problem there is that by default, the IDE wants to autocomplete like this:
<%# taglib prefix="showMore" tagdir="/docroot/WEB-INF/tags/wj/showmore" %>
I simply made a web facet and said that when I type /WEB-INF I really want you to look at /docroot/WEB-INF. This small change saves me so much headache throughout the day. I also did the same thing with /html instead of /docroot/html.
You know how in eclipse when you go to an init.jsp file in the ext environment it always freaks about the init.jsp includes from portal because it can't find it? I once again used a web facet (called portal-web) and I linked / to /liferay/builds/4.3.x/portal-web/docroot. Now when I say:
<%# include file="/
right there it starts autocompleting. It lists out everything in both portal-web and ext-web for me, because I have access to both inside Liferay! Doing this then:
<%# include file="/html/common/init.jsp" %>
it's a problem, I can even click init.jsp and press ctrl (cmd) + b and it will jump me right into the init file in portal.
I tend to run IntelliJ with one "Module" and two "Content Roots". One content root is portal, the other is ext. The plus here is that the debugger knows about the portal source files, and I can easily jump between jsp files as well.
Resource Link:
Why I use IntelliJ instead of Eclipse?

Related

NoClassDefFoundError in JSP in spite of class placed in /lib

My JSP instantiates a MovieDetails class. I am currently working using scripting to ensure everything is working fine, before I move to tags.
I am using Tomcat 8.0. Directory structure for MovieDetails.class: com/library/model/beans. A copy of the directory structure with the MovieDetails.class file is also placed under WEB-INF/lib (have tried putting a .jar for the file too)
In the JSP I have:
<%# page import="com.library.model.beans.*" %>
And later:
<%
MovieDetails movDet = (MovieDetails)request.getAttribute("MovieDetailsBean");
....
....
%>
I am getting:
java.lang.NoClassDefFoundError: com/library/model/beans/MovieDetails
Can anyone please tell me why the JSP can't find the class in spite of the class being in the /lib directory?
NoClassDefFoundError and ClassNotFoundException is two different exception

Linking Files And Directory Structuring

I'm working on a web project using java/ jsp/ servlets/ html/ css in eclipse tomcat, where everything is in the WebContent folder.
In my jsp files... When I try to include other jsp files (using a link like "/fileName.jsp" in jsp include directive) I can do that successfully.
But When I try to include image files (using a link like "/fileName.jpg" in the <img src=""> tag) nothing happens.
Nothing happens because instead of looking in the WebContent folder for image file it looks in the tomcat home directory, i.e.
Instead of looking at "http ://localhost:port/projectName/..." it looks at "http: //localhost:port/..."
Why does it look at the wrong location only with <img src=""> tags but not in <%# /> tag.
A workaround for this is that I start giving absolute paths "/projectName/..." However doing this means I'm hardcoding project name everywhere. This is what I do not want.
Don't include binary content in an ascii output. Why not just use the img tag? If you need to do something to produce a jpeg, I would use a Servlet.
Because the jsp-Links in the website are getting processed and the image links not. Either change the image path or develop an filter that changes the images'links.
Yes Templar, that could have been a way to solve my problem.
However, I simply changed the Context Root of my project from "Project Name" to "/" in Eclipse. This solved my problem.

directory folder name to put user defined packages in tomcat 7

I am new to java and jsp. I have just created a small package and that package contains a simple java class. Let me show what i really want to do. I am not working with servlets so please tell me about this simple example so that i can go ahead with my work. Below is my code or a package class and a jsp page.
my java package
package mypack; //this is my package
public class Abc{
public Abc(){
}
public void message(){
System.out.println("My first java package");
}
}
index.jsp this is my jsp page where i need to use this package
<%# page import="java.sql.*"%>
<%# page import="mypack.*"%>
<%
Abc a = new Abc();
a.messsage();
%>
I was using JDK1.5 and tomcat 3. But i want to update my system. Now i have JDK1.7.0_11 and tomcat 7. I know the path of tomcat 3 to put my packages but i don't know where to put these packages in tomcat 7.
tomcat 3 directory path to place packages:
D:\Java\tomcat\examples\WEB-INF\classes //i put my package at this path in tomcat3
tomcat 7 directory path to place packages:
D:\web\Tomcat 7.0\webapps\ROOT\WEB-INF //trying to put my package in here but no use.
I could not find the classes folder under the direcoty WEB-INF in tomcat 7. I made a folder myself named classes inside of WEB-INF but it does not work. Even i have deleted that my "classes folder" and put my package in WEB-INF, but it does not work. Please tell me the path where i can put my java package in tomcat server 7.
I have placed my jsp page in here:
D:\web\Tomcat 7.0\webapps\ROOT\a //folder "a" contains my jsp file. index.jsp and
its working
Problem is, jsp page could not find the package. Please help me out with this.
packages are always located in WEB-INF/classes, not directly in WEB-INF.
Try to put them here thus:
D:\web\Tomcat 7.0\webapps\ROOT\WEB-INF\classes

Problems referencing an external project from my jsp - "ExternalClass cannot be resolved to a type"

I inherited 2 JSP projects (tomcat) in my eclipse with common code that is duplicated.
I wanted to externalize the duplicate code to common library/project and reference it from both JSP project.
So I did the following:
I created a new Java project "JSP-Common" with the following package "com.mycompany.jsp.common". There I create a class "ExternalClass" with a public "test()" method that returns a string.
in JSP1 project(one of the 2 JSP projects):
I added the JSP-Common project to the build path projects tab
I added to the jsp file in it the following import:
<%# page import = "com.mycompany.jsp.common.*" %>
I added to the jsp file in the body somewhere
<% ExternalClass ec=new ExternalClass(); %>
After building and publishing the project, I get a "ExternalClass cannot be resolved to a type" error on the line with the instantiation above in my page.
Am I missing something ? Help ?
I want to make it so when I build my JSP1 project it will automatically take the callses from the JSP-Common project.
You have to add JSP-Common project in Deployment Assembly: go to Deployment Assembly page under project properties. Hit Add and select "Project" option.

Deploying a jar in Jboss

I have some .jspf files that are fragments which I include in a new .jsp file. The reason they are fragments is that they are reused across multiple jsps with some additional components.
My issue now is that I want to use these .jsps in 2 different .war files.
So I created a new .jar file which includes these jspf, now I am trying to deploy this jar so that I am then import it in my new jsp which is inside 1 of the 2 wars.
I am not able to deploy the jar succesfully in Jboss 4.2. I am using Eclipse ide.
Any ideas on this? If there is an alternate approach I would appreciate any ideas.
Thanks for this suggestion, I have followed this idea and deployed my war1 which contains my .jspf files.
So in my war2 on a jsp I do:
<c:import context="/sharedComponents" url="/easyPayNamePaymentOption.jspf" var="easyPayName"/>
<%# include file="easyPayNamePaymentOption.jspf" %>
However I am not able to render this page with the included fragment, I am not sure how to address the imported jspf. I ahve tried several different ways like:
<%# include file="/sharedComponents/easyPayNamePaymentOption.jspf" %>
and also using var name like:
<%# include file="#{easyPayName}" %>
However it keeps looking inside the current war. How can I tell it to include the newly imported fragment and display it?
Thanks in advance.
I can see what you're trying to do, but this isn't going to work with a JAR file. All JSP files (including JSPF) have to inside a WAR, not a JAR.
The simplest solution is to put copies of the JSPF files into each WAR that needs to use them. Assuming that you don't want to do this, then there is an alternative, called a cross-context WAR.
By default, JBoss allows its webapps to request resources from each other. For example, say webapp1 (context path /app1) wants to import JSPF /my.jspf from webapp2 (context path /app2). You can use JSTL to do this, from inside webapp1:
<c:import context="/app2" url="/my.jspf"/>
So if you were to create a "shared" WAR file containing your JSPF files, and deployed this to JBoss, then your other webapps could use the above technique to include the contents of the JSPFs into their own JSPs.
edit: I've read your updated question, and I don't understand why you added var="easyPayName" to the <c:import> tag. All that's doing is importing the contents of easyPayNamePaymentOption.jspf and storing it in a variable called easyPayName, which seems completely unnecessary.
I think perhaps you're associating <c:import> with a java import? If so, don't - they're completely different. <c:import> should really have been called <c:include>, because that's what it does.
Just keep it simple, remove the attribute, and just have
<c:import context="/sharedComponents" url="/easyPayNamePaymentOption.jspf"/>
That's all you need to do - it will include the content of easyPayNamePaymentOption.jspf directly in the JSP.

Categories

Resources