Extended JSP taglib - java

Is there any extended JSP taglib available? Basically, because of project constraints, we're using a home-grown framework and working with JSPs. We want a taglib that will help us work easily with html forms and form elements and provide some sort of binding. Something like struts html tablib. I'm not sure if we can use the struts taglib standalone?

The best solution I can provide for you is to use a retired tag library from jakarta.
It still works but it is no longer supported or maintained by them.
The link can be found here
The one you will be interested in is the "input" tag library which has tags for the following fields:
form
text
password
textarea
hidden
select
option
radio
checkbox

There should be no problem using Struts with appropriate JAR files in Web App lib folders (put all the Struts JARs in there)

Related

Image is not showing in JSP File

I am having problems displaying an image in my .jsp file.
The image no_image.jpg is located inside the following directory of my Spring MVC application:
SpringProject\src\main\webapp\assets\images\no_image.jpg
I am trying to access it through my .jsp file like this:
<img src="${pageContext.request.contextPath}/assets/images/no_image.jpg"></a>
Project structure (using apache Netbeans IDE 11):
However this does not seem to display the image, does anyone have any ideas why?
Probably the Expression Language is treated as plain text. To tell the JSP engine to process such expressions, add <%# page isELIgnored="false"%> before the <html> tag in your JSP page (at the top, where you have other "%#"-like directives or JSTL imports). Unfortunately, this parameter's value defaults to true.
I have found the solution. For anyone else experiencing this issue, a possible solution is to put the following into your spring configuration .xml file: (Where you define your base package for the project, my one is named spring-config.xml)
<mvc:resources mapping="/assets/**" location="/assets/"/>

Best way to include JavaScript in a JSP taglib jar

I'm building a taglib with a few custom tags. Some of these require a bit of JavaScript to work properly. What would be the best way to add the JavaScript so that it's included in the taglib jar?
The most obvious approach to me would be to put it into the custom tags' doStartTag() method like this pageContext.getOut().print("<script>my js</script>");, which I don't like for 2 reasons: It looks absolutely horrible and it gets added every time the tag gets used on a page.
I'm looking for a solution that lets me keep the JavaScript in a separate .js file that rests somewhere in the taglib jar and gets included only once per page whenever a custom tag is used.

Using a function from one jsp in another jsp

I have a jsp file were the db connection is created and queries can be executed.
I want to call these functions (of connecting to a database and executing query) from another jsp simply to add new record to a database and to stay on a same page.
I read here about an "import" tag but i don't have any packages. Also i'm not allowed to use JSTL.
Little help?
Thanks for considering my question.
You can use <%#include %> directive to statically include JSP fragments.
<%#include file="db.jsp" %>
Writing JSPs this way is however considered poor practice. Java code belongs in a Java class, not in a JSP file. You could for example use a preprocessing servlet class wherein you do the job in doGet() method and have a reuseable DAO class wherein you hide all the JDBC boilerplate code away.
You shouldn't program in your jsp. Put that connection logic out into a class and use that in your jsps.
Afterwards you can import your class with:
<%# page import="package.yourclass" %>
In general you should consider using frameworks like Spring MVC or Struts. Using JSF could be another option.
Mainly we used to import two types of pages into a jsp page,
Java pages
JSP pages
If you want to import a java page into your jsp page, syntax is
<%# page import="package_name.java_page" %>
If you want to import a jsp page, syntax is
<%# include file='jsp_page_name.jsp'%>
while adding a jsp page, if it is in an another folder, you just need to specify the paths also.

how to modify html in a struts...links to jsp

I am new to struts and I am not sure of jsp either. I am a php scripter with JS and C# experience and various other language including java but not the struts or jsp component.
I am confused as to how I can modify the html of a struts file once I have found the config in xml and the jsp linking to it.
JSPs are typically where you would look for HTML. So, you can usually just open the JSP and edit the HTML.
However, if the HTML is being generated dynamically, you may not see it there. There should be a Tag or Java Scriptlet that should give you some indication of where the HTML is coming from, though.

Is there something like ASP.NET web user controls in Java/struts/jsp

In java/struts/jsp world, is there something like the ASP.NET custom user control, a piece of UI with back end code that performs a specific logic, and can be pulled and used in another place (for example something like the login control).
The current Java EE API offers JSF 2.0 on Facelets for this. Note that Struts(2) is actually a competitor to JSF and that JSP is a view technology like ASP, not a MVC framework. As per Java EE 6 / JSF 2.0, JSP is been replaced by Facelets as the default view technology.
On top of the basic JSF implementation you can choose from a lot of "rich" component libraries which adds an extra CSS and Ajax sausage, for example PrimeFaces (showcase) and OpenFaces (showcase).
See also:
What is the mainstream Java alternative to ASP.NET?
What is the difference between JSP, Servlet and JSF?
Java EE 6 tutorial - chapters 4-10
Coreservlets.com JSF 2.0 tutorial
What you are after is called tag files in JSP. I found that they are not as easy to learn and use as .NET user controls.
You can use them similarly to user controls .NET.
workflow is like this.
Create a tag file in web-inf/tags/AtillaTagLibrary/DropDownList.tag
reference your tag file in your jsp file like below
<%# taglib prefix="ct" tagdir="/WEB-INF/tags/AtillaTagLibrary"%>
Use your tag file like normal jsp tag.
<ct:DropDownList />
This tag files can get attribute values from outside like user controls.

Categories

Resources