Johnnie Smith

Greenhorn
+ Follow
since May 02, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Johnnie Smith

Hello,

In Struts 2 there are ServletContextAware and ApplicationAware interfaces.

If my action class implements the ApplicationAware and sets an attribute in the map, the values of this attribute can be fetched on the result JSP by typing <c:out value="${application.attributeName" /> where application is the JSP implicit object.


Similarly, if my action class implements the SevletContextAware interface and sets the attribute by using the setAttribute() method, the values of this attribute too can be fetched on the result JSP by typing <c:out value="${application.attributeName" /> where application is the JSP implicit object.

My question is: What is the difference in purpose between the 2 interfaces? In both cases, any attribute that is set, is in application scope and accessible across pages of the application.

I tried reading documentation on the web regarding the difference in the purpose of the two interfaces and it looks like as far as setting attributes is concerned, in both cases, the attributes are set in application scope.

Regards,
John.

9 years ago
Hello,

I have read in the struts documentation about 2 validators required and requiredstring.

The required validator checks whether a field value is null.

The requiredstring validator checks whether a field value is null and also whether the field value is empty.

So the required validator will not find any error if a blank value is passed as the field value but requiredstring validator will not allow blank value to be submitted.

I have the following doubt: If the required validator is anyway going to allow blank values to be submitted what is the use of this validator? Suppose I have a field named userName where the required validation is carried out.

<field name="userName">
<field-validator type="required">
<param name="trim">true</param>
<message>Username must not be null</message>
</field-validator>
</field>


Under what circumstances will I get an error message?

Regards,
John

9 years ago
I have a bean called Person with properties firstName of type String, lastName of type String and dob of the type java.util.Date with their setter and getter methods.

My action class is named TransactionAction and it extends ActionSupport. In my action class, I declare a Collection named "persons" with its getter and setter methods. I also have an execute() method that returns SUCCESS.

I want to allow a user to enter a date in dd-MM-yyyy format. I have already written a converter for it, registered the converter in my TransactionAction-conversion.properties file and it works fine for a single object.

My question is: How do I register the above converter for the date field of every Person object in a collection of persons? Please note that the collection contains many objects of type Person. I want to use the converter for the date field of each object in the collection.

Thanks & Regards,
John Smith.
10 years ago
I have a HTML form in a Struts application that accepts data pertaining to a billing process. On this form there is a field called amount.

I want to accept a double value for this field and assign it to a bean property named amount of type double.

I am attempting to write my own custom type converter that will accept a double value with a comma present in it like 8,567.00.

I am extending DefaultTypeConverter and overriding its convertValue() method.

As per document at the following location "http://commons.apache.org/proper/commons-ognl/apidocs/org/apache/commons/ognl/DefaultTypeConverter.html", this method has the following signature:

public <T> T convertValue(Map<String,Object> context,Object value,Class<T> toType)

So I wrote the following code:

package mypack;

import java.text.*;
import java.util.*;
import ognl.DefaultTypeConverter;

public class DoubleConverter extends DefaultTypeConverter {

public <T> T convertValue(Map<String,Object> context,Object value,Class<T> toType)

{
if (toType == String.class)
{
NumberFormat formatter = new DecimalFormat("#,##0.00");
return toType.cast(formatter.format((Double)value));
}
else if (toType == Double.class || toType == Double.TYPE)
{
try
{
String s[]=(String[])value;
s[0]=s[0].replaceAll(",","");
return toType.cast(Double.parseDouble(s[0]));
}catch(NumberFormatException e)
{
System.out.println(e);
}
}
return null;
}
}

I am getting the following compile-time error.

Name clash: The method convertValue(Map<String,Object>, Object, Class<T>) of type DoubleConverter has the same erasure as convertValue(Map, Object, Class) of type DefaultTypeConverter but does not override it


I have taken the signature of convertValue() that is specified in the above-mentioned document.

So I am not able to understand why I am getting this error and why the message says that "but does not override it" when I am clearly overriding the superclass' convertValue() method.

As far as I know, this error should arise only in case there are 2 overloaded methods that are supposedly distinct have the same erasure. Here I am not writing any other method.

Please shed some light on this problem.


Versions
jdk: 1.6 Update 14
Tomcat: 6.0
Struts: 2.3.15.1

Thanks & Regards,
John
10 years ago
Hello Joe.

I tried your page and it worked like a charm. Now my own page works fine too.

I do not know what went wrong earlier.

Anyway, Thank you very much.

Regards,
John.

10 years ago
Hello Joe,

The problem is with the set tag.

The value of identification_number is already set in the execute() method of the action class and it is of the type double.

Because the name identification_number is so long, I want to assign its value to a variable called "id" on the JSP and use it.

My query is: Is there any technique of using the <s:set> tag to assign numeric value to the variable "id"? Or is it necessary that the value we are assigning should strictly be a String value?

Thanks & Regards,
John.




10 years ago
Thank You Joe.

Yes I want to assign the value of identification_number to a variable id. But identification_number is of the type double in action class.

So I want to know how to assign a numeric value to a variable on the JSP. I had gone through the documentation before posting the question.

The example that is present there is:

<s:set name="personName" value="person.name"/>
Hello, <s:property value="#personName"/>. How are you?

The name of the person is of type String so this will not create any problem. Is there any technique of using the <s:set> tag to assign numeric value to the variable? Or is it necessary that the value we are assigning should strictly be a String value?

Regards,
John
10 years ago
Hello,

I am using Struts 2.3.x. I have an action class in which there is a private variable "identification_number" of type double with its public setter and getter methods. The getter method returns double.

In the execute() method, I set the value of this variable to 2155.

On my JSP, I want to repeatedly use this variable so I prefer a shorter name "id".

So when I tried to use <s:set var="id" value="identification_number" /> I got the following exception:

Struts Problem Report
Struts has detected an unhandled exception:

Messages: java.lang.Double cannot be cast to java.lang.String

My question is how to use the set tag for numeric data types?

Regards,
John.


10 years ago
Hello I am trying to implement the MVC architecture using JSP(view), Servlet(Controller) and Java classes(Model).

I have named my Application as First_App.In this application there are 2 JSPs namely First.jsp and Second.jsp.


To implement the controller, I have written a servlet whose job is to analyse a request for any page in this application and perform the following tasks:

1. Create an object of the model class,
2.Call one of its methods that will fetch data from a database,
3. Create an instance of a bean
4. Call its respective setter methods to set the attribute values with the retrieved data
5. Set the bean as a request attribute.

After doing all this, it uses the following code:

getServletConfig().getServletContext().getRequestDispatcher("First.jsp").forward(request,response);

or

getServletConfig().getServletContext().getRequestDispatcher("Second.jsp").forward(request,response);


to forward the request to either of the JSPs depending on some criteria.

The web.xml file contains the following entry:


<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>mypack.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyFirstApp/*</url-pattern>
</servlet-mapping>

The problem that I am facing is that data does not get populated on the JSP. On the JSP, I retrieve the brean from the request and display its attribute values.

Please note that I want to map any request for any resource within this web application to the servlet because I want the servlet to forward it to the particular JSP.

I believe that this is the essence of MVC. But what happens is that the forwarded request again goes through the controller servlet

and again the processing is done.

How do I code the controller servlet in such a manner that a user request passes through it but the forwarded request from within the servlet's post method is ignored

and the concerned JSP is displayed?

Version of Tomcat: 6.26
JSP:2.1
Servlet: 2.5
JDK:1.6

Regards,
John.
10 years ago
JSP
Yeah, I tried by removing all filter and listener mappings in web.xml file. I also tried using different browsers (Chrome & Internet Explorer). But <jsp:attribute> action does not work on my JSP.

11 years ago
JSP
Hello Jelle,

My file structure is mentioned below:

The Fruit.jsp file

E:\tomcat\webapps\ROOT\fruitpack\Fruit.jsp


The MyTag.tag file

E:\tomcat\webapps\ROOT\WEB-INF\tags


The URL that I type in my browser is:

http://localhost:8080/fruitpack/Fruit.jsp

The web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<display-name>
Welcome to Tomcat</display-name>

</web-app>

My web.xml file contains mappings for filters, listeners and servlets. I have not indicated the mappings in this post as I have not used web.xml file for this particular page. I mean, I have placed my tag files in the 'tags' directory and have mentioned the directory path on the JSP as the value of the taglib's 'tagdir' attribute.


I also want to point out that my other tags where <jsp:attribute> is not used are working fine.

Regards,
John.
11 years ago
JSP
Hi Jelle, Thanks for pointing this out. Actually this particular error was a typing error. I have edited my question. I am still getting the error that I had originally mentioned.

Regrads,
John
11 years ago
JSP
Hi, I am trying to use the <jsp:attribute> action in my program.

In my program I want to pass an attribute value from a JSP to a tag file.

My JSP is as follows:

The Fruit.jsp file

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>

<c:set var="f" value="apple" />

<t:MyTag>
    <jsp:attribute name="fruit">
      ${f}
    </jsp:attribute>
</t:MyTag>

The MyTag.tag file

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@attribute name="fruit" %>


<c:out value="${fruit}" />
<jsp:doBody />


I am using Apache Tomcat 6.0.26 Server. When I type the url in the browser, I get the following error:

Fruit.jsp(7,9) jsp:attribute must be the subelement of a standard or custom action.

To the best of my knowledge, the MyTag tag is my custom action.

Can somebody assist me to identify what the problem is?

Regards,
John
11 years ago
JSP
Hi, Assuming that you know the Java programming language properly and have experience of working in it, it will take you around 2 months to study JSP and Servlets systematically.

Regards,
John
Hello, I want to know the difference between <jsp:invoke> and <jsp:doBody> actions in JSP 2.0. According to the specification, both perform the same task of invoking the body of a tag. But <jsp:doBody> operates on the body of the tag instead of on a specific fragment passed as an attribute.

I have used <jsp:doBody> successfully but I have not yet managed to understand the meaning of "specific fragment passed as an attribute".

Can somebody elaborate more on the use of <jsp:invoke> and explain the difference between the 2 actions, perhaps with the help of a small example?

Thanks & Regards,
John.
11 years ago
JSP