Accessing java class and methods within JSP
Hi All,
I am writing a JSP page, and need to instantiate an object of a certain class within my package, in my JSP page.
How do I do that? Do i use the <%import = "Whatever.class" %>
Any help would be greatly appreciated.
Kal.
Hi.. Thanks for ur reply.
I am actually having a problem with the import statement. So the part where you have mypackage.Myclass, it is not working for me.
I am using Sun Forte.. and the directory is myprojects/test/Classes/MyClass.java
so how would the import statement look like?
Thanks
Kal.
Using directive for import statements:
<%@ page
import="java.util.*,java.sql.*,mypackage.MyClass" %>
Then use scriptlet:
<%! MyClass c= new MyClass(); %>
<%! private void method() {...} %>
<%= c.getxxx() %>
...and so on
Similar Messages
-
Call Java Class and Methods from ABAP
Hi
I install de JCo, But how i can call java class and methods from ABAP?. somebody has an example?
The tutorial say that's is possible, but don't explain how do that?
Thanks.
Angel G. HurtadoIf you need only simple java program, you do not need to install JCo. The following codes can call java class.
DATA: COMMAND TYPE STRING VALUE 'C:\j2sdk1.4.2_08\bin\java',
DIR TYPE STRING VALUE D:\eclipse\workspace',
PARAMETER TYPE STRING VALUE 'Helloworld'. "here the name of your java program
CALL METHOD CL_GUI_FRONTEND_SERVICES=>EXECUTE
EXPORTING
APPLICATION = COMMAND
PARAMETER = PARAMETER
DEFAULT_DIRECTORY = DIR
MAXIMIZED =
MINIMIZED = 'X' "If you need the DOS window to be minimized
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
BAD_PARAMETER = 3
FILE_NOT_FOUND = 4
PATH_NOT_FOUND = 5
FILE_EXTENSION_UNKNOWN = 6
ERROR_EXECUTE_FAILED = 7
OTHERS = 8.
Tell me if it works.
Nuno. -
How can we call java control source methods within jsp
Hi guys!
How can we access java control source methods that are defined in java control projects within jsp.I am getting error when I tried to call java control source's methods in scriptlet of JSP page.Any help/suggestion is appreciated.
Thanks
-ChanduUh, what?
(And you are sure that it's not a Weblogic-specific question?) -
Java compilation and running within jsp page
hi,
i need of a jsp page that has a textarea in which the java coding are entered and this coding is compiled and executed. The execution result is printed.
Please guide me.I don't think you can invoke java compiler or run time environment at client side (browser). Even if you use applet, i think you will not be able to do it at client side. Moreover, though java virtual machine will be available in your target user's browser, but you should not expect a java compiler in users machine.
I have seen exam sites where they have added java code compilation and execution ability. To achieve this you can do:
1. In your JSP page use text area where user can edit java code.
2. Provide two buttons - 'Compile' and 'Run'.
3. On click of 'Compile' send the code to server side. In server machine create a file in a temporary location in disk and save the code content received from client.
4. Invoke 'javac' command (as a separate process) to compile the java class created in step 3. This process will give you error/output back which you can send back to user.
5. In case of 'Run' you must first invoke above mentioned 'Compile' service and then another service ('Run') to actually run you java class. You can build 'Run' service in similar way.
Here you challenge would be managing multiple client requests (to compile and run java class) at the same time.
Code to compile and run java classes:
import java.io.*;
* A class to compile and run java classes.
* @author Mrityunjoy Saha
public class JavaProcessor{
public static void main(String[] args){
// Option 1 to compile and option 2 to run.
int option=1;
if(args.length > 0){
option=Integer.parseInt(args[0]);
JavaProcessor p=new JavaProcessor();
if(option==1){
p.compile();
} else {
p.run();
public void run(){
String file="FoodTest";
String directory="C:/Users/mrityunjoy_saha/Documents/Test";
executeCommand("java", directory, file);
public void compile(){
String file="FoodTest.java";
String directory="C:/Users/mrityunjoy_saha/Documents/Test";
executeCommand("javac", directory, file);
public void executeCommand(String command, String directory, String file){
try{
String[] envp=null;
// Make sure that you have done Java setup in your machine.
// To test this try invoking javac command in command prompt.
Process p=Runtime.getRuntime().exec(command+" "+file,envp,new File(directory));
BufferedReader errorStream=new BufferedReader(new InputStreamReader(p.getErrorStream()));
String readData="";
while((readData=errorStream.readLine())!=null){
System.out.println(readData);
BufferedReader outStream=new BufferedReader(new InputStreamReader(p.getInputStream()));
readData="";
while((readData=outStream.readLine())!=null){
System.out.println(readData);
catch(IOException ioe){
ioe.printStackTrace();
}Thanks,
Mrityunjoy -
Class and Methods in JSP Scriptlets?
Is it possible to declare functions or classes in a JSP scriptlets? If so, what would be the proper syntax in doing so?
Use the exclamation mark.
<%!
public void doSomething() {
// Implement.
%>Although rather use servlets or beans for business logic and use JSP for display only. -
What is wrong with this Java class and method?
Created a managed bean and following method as shown below.
public void setEvent(DisclosureEvent disclosureEvent, CoreShowDetail detail) {
if (disclosureEvent.isExpanded()) {
detail.setDisclosed(!detail.isDisclosed());
-- Then integrated above in the following jspx.
<af:showOnePanel inlineStyle="width:400px;height:300px;"
binding="#{ShowApanel.showOnePanel1}"
id="showOnePanel1">
<af:showDetailItem text="ADF Faces Components"
binding="#{ShowApanel.showDetailItem3}"
id="showDetailItem3" disclosureListener="#{ShowApanel.setEvent}">
<af:panelHeader text="ADF Faces Components First Child"
binding="#{ShowApanel.panelHeader6}"
id="panelHeader6"/>
<af:panelHeader text="ADF Faces Components Second Child"
binding="#{ShowApanel.panelHeader5}"
id="panelHeader5"/>
</af:showDetailItem>
<af:showDetailItem text="Architecture"
binding="#{ShowApanel.showDetailItem2}"
id="showDetailItem2"
disclosureListener="#{ShowApanel.setEvent}">
<af:panelHeader text="Architecture First Child"
binding="#{ShowApanel.panelHeader4}"
id="panelHeader4"/>
<af:panelHeader text="Architecture Second Child"
binding="#{ShowApanel.panelHeader3}"
id="panelHeader3"/>
</af:showDetailItem>
</af:showOnePanel>
THE ISSUE:[b]
I am still not able to programmatically disclose or undisclose the panel component.
Where am I going wrong? Please help.
Thanks,
RuchirHello Frank,
I put several print statments but none of them is executing on browser. Does this mean that the method is not being called even once?
public void setEvent(DisclosureEvent disclosureEvent, CoreShowDetail detail) {
System.out.println("HelloServer Exiting ...");
if (disclosureEvent.isExpanded()) {
System.out.println("HelloServer Exiting1 ...");
// detail.setDisclosed(!detail.isDisclosed());
System.out.println("HelloServer Exiting2 ...");
Thanks,
Ruchir -
Using java class and variables declared in java file in jsp
hi everyone
i m trying to seperate business logic form web layer. i don't know i am doing in a right way or not.
i wanted to access my own java class and its variables in jsp.
for this i created java file like this
package ris;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class NewClass{
public static void main(String args[]){
Connection con = null;
ResultSet rs=null;
Statement smt=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
smt=con.createStatement();
rs= smt.executeQuery("SELECT * FROM emp");
while(rs.next()){
String str = rs.getString("Name");
}catch( Exception e){
String msg="Exception:"+e.getMessage();
}finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}next i created a jsp where i want to access String str defined in java class above.
<%--
Document : fisrt
Created on : Jul 25, 2009, 3:00:38 PM
Author : REiSHI
--%>
<%@page import="ris.NewClass"%>
<%@page contentType="text/html" 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>JSP Page</title>
</head>
<body>
<h1><%=str%></h1>
</body>
</html>I wanted to print the name field extracted from database by ResultSet.
but it gives error cannot find symbol str.
please help me to find right way to do this.
i am using netbeans ide.Very bad approach
1) Think if your table contains more than one NAMEs then you will get only the last one with your code.
2) Your String is declared as local variable in the method.
3) You have not created any object of NewClass nor called the method in JSP page. Then who will call the method to run sql?
4) Your NewClass contains main method which will not work in web application, it's not standalone desktop application so remove main.
Better create an ArrayList and then call the method of NewClass and then store the data into ArrayList and return the ArrayList.
It should look like
{code:java}
package ris;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class NewClass{
public static ArrayList getNames(){
Connection con = null;
ResultSet rs=null;
Statement smt=null;
ArrayList nameList = new ArrayList();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
smt=con.createStatement();
rs= smt.executeQuery("SELECT * FROM emp");
while(rs.next()){
nameList.add(rs.getString("Name"));
return nameList;
}catch( Exception e){
String msg="Exception:"+e.getMessage();
</code><code class="jive-code jive-java"><font>return nameList;</code><code class="jive-code jive-java">
}finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
</code><code>return nameList;</code>
<code class="jive-code jive-java"> } -
How do I access classes and methods defined in a wsdl file
I have been provided a wsdl file I need to find out how do I access classes and methods defined in a wsdl file directly instead of doing a wsdl2java...
Several comments :
1- is there any reason to have blank chars inserted after the path ? Seems that you already have a problem there. If possible, try to solve the problem at the source
2- the end of line char is usually CR (Carriage Return, aka ASCII char 13 = $0D = Control-D). But LF (Line Feed = 10 = $0A = control-A) is also used (platform dependent). In LV, you can use the "Concatenate strings" function to add/insert control chars (found in the String Control Palette). However, this will not solve your problem of unwanted added blank chars at the end of your string.
3- you can use the Trim white space.vi (in the "Additionnal string functions" sub-palette) to remove ALL the spaces in your string
4- you can build your own "end space remover" function. :
reverse the string, wire to a "Match pattern" function, use " +" (space + "+") to search for any number of spaces, reverse again the "after substring".
5- there is no point 5 :-)
You may find interesting description of ASCII chars here
Chilly Charly (aka CC)
E-List Master - Kudos glutton - Press the yellow button on the left... -
Java abstract classes and methods
Can anyone please tell me any real time example of abstract classes and methods.
I want to know its real use. If anyone have ever used it for some purpose while programming please do tell me.Ashu_Web wrote:
No please.. I just want to know if you have used it while programming. Like "an abstract class can be used to put all the common method names in it without having to write actual implementation code."That would describe an Interface better than an abstract class. Abstract classes usually have at least some implementation.
I want to know its usage in programming, not just a definition. I guess you understand what I am looking for.Yes, and I gave you one: java.util.AbstractList. It can be found inside the src.zip in your JDK directory and it is a pretty good example for an abstract class that provides some implementation and defines exactly what is necessary to make a full List implementation. -
Difference b/w Java Class and Bean class
hi,
can anybody please tell me the clear difference between ordinary java class and java Bean class. i know that bean is also a java class but i donno the exact difference between the both.
can anybody please do help me in understanding the concept behind the bean class.
Thank u in advance.
Regards,
FazlinaWhile researching this question, I came across this answer by Kim Fowler. I think it explains it better than any other answer I have seen in the forum.
Many thanks Kim
Hi
Luckily in the java world the definition of components is a little
less severe than when using COM (I also have, and still occasionaly
do, worked in the COM world)
Firstly there are two definitions that need to be clarified and
separated: JavaBean and EnterpriseJavaBean (EJB)
EJB are the high end, enterprise level, support for distributed
component architectures. They are roughly equivalent to the use of MTS
components in the COM/ COM+ world. They can only run within an EJB
server and provide support, via the server, for functionality such as
object pooling, scalability, security, transactions etc. In order to
hook into this ability EJB have sets of interfaces that they are
required to support
JavaBeans are standard Java Classes that follow a set of rules:
a) Hava a public, no argument constructor
b) follow a naming patterns such that all accessor and modifier
functions begin with set/ get or is, e.g.
public void setAge( int x)
public int getAge()
The system can then use a mechanism known as 'reflection/
introspection' to determine the properties of a JavaBean, literally
interacting with the class file to find its method and constructor
signatures, in the example above the JavaBean would end with a single
property named 'age' and of type 'int' The system simply drops the
'set' 'get' or 'is' prefix, switches the first letter to lower case
and deduces the property type via the method definition.
Event support is handled in a similar manner, the system looks for
methods similar to
addFredListener(...)
addXXXListener
means the JavaBean supports Fred and XXX events, this information is
particularly useful for Visual builder tools
In addition there is the abiliity to define a "BeanInfo' class that
explicitly defines the above information giving the capability to hide
methods, change names etc. this can also be used in the case where you
cannot, for one reason or another, use the naming patterns.
Finally the JavaBean can optionally - though usually does - support
the Serializable interface to allow persistence of state.
As well as standard application programming, JavaBeans are regularly
used in the interaction between Servlets and JSP giving the java
developer the ability to ceate ojbect using standard java whilst the
JSP developer can potentially use JSP markup tags to interact in a
more property based mechanism. EJB are heaviliy used in Enterprise
application to allow the robust distribution of process
HTH.
Kim -
I'm kind of new to this so please bear with me. I'm writing some JSPs within the portal and can't find the JPDK classes and methods, does anybody know where these are? I'm tring to get the IP address of portal visitors for security reasons. Thanks in advance for the help!
The JPDK was not shipped with older versions of the portal - it was only available via download from OTN.
Oracle9iAS release 1.0.2.2 will be the first release to include the jpdk in the base release. -
Hi Experts,
I am studying ABAP Objects, before that I need to know How to use the exsiting classes and Methods in our program and how to search for particular class and methods?
If it explanied with example well and good.
Thanks
saiHi Saikar,
Here i am sending you very useful content for the usage of classes and its methods.
It helped me a lot.
If you find it useful then do not forget to award points.
Table of Contents
Applies to:......................................................................................................................................1
Summary........................................................................................................................................1
Author Bio......................................................................................................................................1
Main Class CL_SALV_TABLE......................................................................................................3
Functions CL_SALV_FUNCTIONS..............................................................................................4
Display Settings CL_SALV_DISPLAY_SETTINGS......................................................................4
Columns CL_SALV_COLUMNS_TABLE and CL_SALV_COLUMN_TABLE..............................5
Sorts CL_SALV_SORTS..............................................................................................................8
Aggregations CL_SALV_AGGREGATIONS..............................................................................10
Filters CL_SALV_FILTERS........................................................................................................12
Layouts CL_SALV_LAYOUT......................................................................................................14
Related Content...........................................................................................................................15
Disclaimer and Liability Notice.......................................................................................................16
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 3
Main Class CL_SALV_TABLE
The main class used to create the simple 2D table is the class CL_SALV_TABLE. Create a reference variable for this class. Create an internal table and fill this internal table with data as show below.
REPORT ZALVOM_DEMO1. data: ispfli type table of spfli. data: gr_table type ref to cl_salv_table. start-of-selection. select * into table ispfli from spfli.
Next we need to create the ALV object for the 2D table. The FACTORY method allows you to create the ALV object in 3 ways. You can create the ALV Grid, as a classical list display, as a full screen grid, and finally embedded into a screen container. For this example, we will be working with the full screen grid. Create the call to the FACTORY method. We are importing the object reference into GR_TABLE and passing the internal table ISPFLI.
cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ).
Next we need to display the grid, for this we use the DISPLAY method . Simply call it.
gr_table->display( ).
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 4
Functions CL_SALV_FUNCTIONS
Next, add functions to the application toolbar. For this, use the CL_SALV_FUNCTIONS class. Create the object reference variable and receive the object using the GET_FUNCTIONS method of the GR_TABLE object. Call the method SET_ALL to force the ALV grid to show all standard functions.
report zalvom_demo1. data: ispfli type table of spfli. data: gr_table type ref to cl_salv_table. data: gr_functions type ref to cl_salv_functions. start-of-selection. select * into table ispfli from spfli. cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ). gr_functions = gr_table->get_functions( ). gr_functions->set_all( abap_true ). gr_table->display( ).
The result is now you have the standard buttons on the application toolbar.
Display Settings CL_SALV_DISPLAY_SETTINGS
Next, we can change some display settings using the class CL_SALV_DISPLAY_SETTINGS. Create the object reference variable and receive the object using the GET_DISPLAY_SETTINGS method of the GR_TABLE object. In this example, we are setting the Striped Pattern for the ALV Grid rows, and setting the heading in the title bar.
report zalvom_demo1.
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 5
). data: ispfli type table of spfli. data: gr_table type ref to cl_salv_table. data: gr_functions type ref to cl_salv_functions. data: gr_display type ref to cl_salv_display_settings. start-of-selection. select * into table ispfli from spfli. cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ). gr_functions = gr_table->get_functions( ). gr_functions->set_all( abap_true ). gr_display = gr_table->get_display_settings( ). gr_display->set_striped_pattern( cl_salv_display_settings=>true ). gr_display->set_list_header( 'This is the heading' gr_table->display( ).
Columns CL_SALV_COLUMNS_TABLE and CL_SALV_COLUMN_TABLE
Next, we can change some of the attributes of a specific column in the ALV grid. In this example we will change the Heading Text of a column as well as the color of a column. Create the object reference variable and receive the object using the GET_COLUMNS method of the GR_TABLE object. This will pass you the object for all columns of the ALV grid. To access just one column, call the method GET_COLUMN from the GR_COLUMNS object. In this example, we are accessing the CITYTO column and the CITYFROM column.
report zalvom_demo1. data: ispfli type table of spfli. data: gr_table type ref to cl_salv_table. data: gr_functions type ref to cl_salv_functions.
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 6
data: gr_display type ref to cl_salv_display_settings. data: gr_columns type ref to cl_salv_columns_table. data: gr_column type ref to cl_salv_column_table.
data: color type lvc_s_colo.
start-of-selection. select * into table ispfli from spfli. cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ). gr_functions = gr_table->get_functions( ). gr_functions->set_all( abap_true ). gr_display = gr_table->get_display_settings( ). gr_display->set_striped_pattern( cl_salv_display_settings=>true ). gr_display->set_list_header( 'This is the heading' ). gr_columns = gr_table->get_columns( ). gr_column ?= gr_columns->get_column( 'CITYTO' ). gr_column->set_long_text( 'This is long text' ). gr_column->set_medium_text( 'This is med text' ).
gr_column->set_short_text( 'This is sh' ).
gr_column ?= gr_columns->get_column( 'CITYFROM' ). color-col = '6'. color-int = '1'. color-inv = '0'. gr_column->set_color( color ).
gr_table->display( ).
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 7 ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 8
Sorts CL_SALV_SORTS
Next, we can add some sorting to the ALV grid. Create the object reference variable and receive the object using the GET_SORTS method of the GR_TABLE object. Next, add the sort by calling the ADD_SORT method of the GR_SORTS object.
report zalvom_demo1. data: ispfli type table of spfli. data: gr_table type ref to cl_salv_table. data: gr_functions type ref to cl_salv_functions. data: gr_display type ref to cl_salv_display_settings. data: gr_columns type ref to cl_salv_columns_table. data: gr_column type ref to cl_salv_column_table. data: gr_sorts type ref to cl_salv_sorts.
data: color type lvc_s_colo.
start-of-selection. select * into table ispfli from spfli. cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ). gr_functions = gr_table->get_functions( ). gr_functions->set_all( abap_true ). gr_display = gr_table->get_display_settings( ). gr_display->set_striped_pattern( cl_salv_display_settings=>true ). gr_display->set_list_header( 'This is the heading' ). gr_columns = gr_table->get_columns( ). gr_column ?= gr_columns->get_column( 'CITYTO' ). gr_column->set_long_text( 'This is long text' ). gr_column->set_medium_text( 'This is med text' ). gr_column->set_short_text( 'This is sh' ). gr_column ?= gr_columns->get_column( 'CITYFROM' ). color-col = '6'. color-int = '1'. color-inv = '0'. gr_column->set_color( color ). gr_sorts = gr_table->get_sorts( ). gr_sorts->add_sort 'CITYTO' ). gr_table->display( ).
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 9 ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 10
Aggregations CL_SALV_AGGREGATIONS
Since we sorted by CITYTO, we can add an aggregation to subtotal the DISTANCE by CITYTO. Create the object reference variable and receive the object using the GET_AGGREGATIONS method of the GR_TABLE object. Next, add the aggregation by calling the ADD_AGGREGATION method of the GR_SORTS object. We also need to modify the call to ADD_SORT to set the SUBTOTAL = ABAP_TRUE.
report zalvom_demo1. data: ispfli type table of spfli. data: gr_table type ref to cl_salv_table. data: gr_functions type ref to cl_salv_functions. data: gr_display type ref to cl_salv_display_settings. data: gr_columns type ref to cl_salv_columns_table. data: gr_column type ref to cl_salv_column_table. data: gr_sorts type ref to cl_salv_sorts. data: gr_agg type ref to cl_salv_aggregations.
data: color type lvc_s_colo.
start-of-selection. select * into table ispfli from spfli. cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ). gr_functions = gr_table->get_functions( ). gr_functions->set_all( abap_true ). gr_display = gr_table->get_display_settings( ). gr_display->set_striped_pattern( cl_salv_display_settings=>true ). gr_display->set_list_header( 'This is the heading' ). gr_columns = gr_table->get_columns( ). gr_column ?= gr_columns->get_column( 'CITYTO' ). gr_column->set_long_text( 'This is long text' ). gr_column->set_medium_text( 'This is med text' ). gr_column->set_short_text( 'This is sh' ). gr_column ?= gr_columns->get_column( 'CITYFROM' ). color-col = '6'. color-int = '1'. color-inv = '0'. gr_column->set_color( color ). gr_sorts = gr_table->get_sorts( ). gr_sorts->add_sort( columnname = 'CITYTO' subtotal = abap_true ). gr_agg = gr_table->get_aggregations( ). gr_agg->add_aggregation( 'DISTANCE' ).
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 11
gr_table->display( ).
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 12
Filters CL_SALV_FILTERS
Using the CL_SALV_FILTERS class we can setup some filters for the data in our ALV GRID. Create the object reference variable and receive the object using the GET_FILTERS method of the GR_TABLE object, and then simply called the method ADD_FILTER with the parameters.
report zalvom_demo1. data: ispfli type table of spfli. data: gr_table type ref to cl_salv_table. data: gr_functions type ref to cl_salv_functions. data: gr_display type ref to cl_salv_display_settings. data: gr_columns type ref to cl_salv_columns_table. data: gr_column type ref to cl_salv_column_table. data: gr_sorts type ref to cl_salv_sorts. data: gr_agg type ref to cl_salv_aggregations. data: gr_filter type ref to cl_salv_filters.
data: color type lvc_s_colo.
start-of-selection. select * into table ispfli from spfli. cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ). gr_functions = gr_table->get_functions( ). gr_functions->set_all( abap_true ). gr_display = gr_table->get_display_settings( ). gr_display->set_striped_pattern( cl_salv_display_settings=>true ). gr_display->set_list_header( 'This is the heading' ). gr_columns = gr_table->get_columns( ). gr_column ?= gr_columns->get_column( 'CITYTO' ). gr_column->set_long_text( 'This is long text' ). gr_column->set_medium_text( 'This is med text' ). gr_column->set_short_text( 'This is sh' ). gr_column ?= gr_columns->get_column( 'CITYFROM' ). color-col = '6'. color-int = '1'. color-inv = '0'. gr_column->set_color( color ). gr_sorts = gr_table->get_sorts( ). gr_sorts->add_sort( columnname = 'CITYTO' subtotal = abap_true ). gr_agg = gr_table->get_aggregations( ). gr_agg->add_aggregation( 'DISTANCE' ).
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 13
gr_filter = gr_table->get_filters( ). gr_filter->add_filter( columnname = 'CARRID' low = 'LH' ). gr_table->display( ).
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 14
Layouts CL_SALV_LAYOUT
If you want to allow the user to manage layouts of the ALV grid, you must use the class CL_SALV_LAYOUT. Create the object reference variable and receive the object using the GET_LAYOUT method of the GR_TABLE object. Then simply call the method SET_KEY with the parameters and set the save restriction using the SET_SAVE_RESTRICTION method.
report zalvom_demo1. data: ispfli type table of spfli. data: gr_table type ref to cl_salv_table. data: gr_functions type ref to cl_salv_functions. data: gr_display type ref to cl_salv_display_settings. data: gr_columns type ref to cl_salv_columns_table. data: gr_column type ref to cl_salv_column_table. data: gr_sorts type ref to cl_salv_sorts. data: gr_agg type ref to cl_salv_aggregations. data: gr_filter type ref to cl_salv_filters. data: gr_layout type ref to cl_salv_layout. data: color type lvc_s_colo. data: key type salv_s_layout_key. start-of-selection. select * into table ispfli from spfli. cl_salv_table=>factory( importing r_salv_table = gr_table changing t_table = ispfli ). gr_functions = gr_table->get_functions( ). gr_functions->set_all( abap_true ). gr_display = gr_table->get_display_settings( ). gr_display->set_striped_pattern( cl_salv_display_settings=>true ). gr_display->set_list_header( 'This is the heading' ). gr_columns = gr_table->get_columns( ). gr_column ?= gr_columns->get_column( 'CITYTO' ). gr_column->set_long_text( 'This is long text' ). gr_column->set_medium_text( 'This is med text' ). gr_column->set_short_text( 'This is sh' ). gr_column ?= gr_columns->get_column( 'CITYFROM' ). color-col = '6'. color-int = '1'. color-inv = '0'. gr_column->set_color( color ). gr_sorts = gr_table->get_sorts( ). gr_sorts->add_sort( columnname = 'CITYTO' subtotal = abap_true ). gr_agg = gr_table->get_aggregations( ).
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 15
gr_filter = gr_table->get_filters( ). gr_layout = gr_table->get_layout( ). gr_layout->set_key( ). gr_table->display( ). gr_agg->add_aggregation( 'DISTANCE' ). gr_filter->add_filter( columnname = 'CARRID' low = 'LH' ). key-report = sy-repid. key gr_layout->set_save_restriction( cl_salv_layout=>restrict_none ).
Related Content
Help - ALV Object Model
Utilizing the New ALV Object Model
SDN ABAP Forum
ALV Object Model Simple 2D Table - The Basics SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com © 2006 SAP AG 16
Disclaimer and Liability Notice
This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.
Regards,
Mandeep.
Note: Award points if contents are useful. -
Trying to run java class files from my jsp
I am having difficulty running java files from my jsp. I have compiled my two java classes and inserted them onto my server in the webapps/WEB-INF/classes/scripting directory and have imported scripting.* into my jsp page. The files both compiled ok before the class files were loaded onto the server but when I rty to run the jsp from the server, I keep getting a compiler error. Thanks in advance
IobHere are the few question
1) Does your java classes which u created is declared under package scripting. *.class files are to be places under the WEB-INF/classes directory only. Not under scripting. But if you java classes say are under a package called a.b. Then you need to put under classes\a\b\. -
Hello,
There is a way to distribute the Mysql db files with yout java application without need to install Mysql in the computer, like in access, you just copy the .mdb file with your java class and create a conection to it.
Thank YouNo. If you want to do that you should just use Access. mySQL files will not work without the mySQL server.
-
Could you please send me the material Opps concepts Classes and Methods
Hi Experts,
I am working on Opps concepts.I am new to this concept.
Could you please send me the detailed presentation on Abap oops.
Thanks inadvance,
Regards,
Rekha.Hi this will help u.
OOPs ABAP uses Classes and Interfaces which uses Methods and events.
If you have Java skills it is advantage for you.
There are Local classes as well as Global Classes.
Local classes we can work in SE38 straight away.
But mostly it is better to use the Global classes.
Global Classes or Interfaces are to be created in SE24.
SAP already given some predefined classes and Interfaces.
This OOPS concepts very useful for writing BADI's also.
So first create a class in SE 24.
Define attributes, Methods for that class.
Define parameters for that Method.
You can define event handlers also to handle the messages.
After creation in each method write the code.
Methods are similar to ABAP PERFORM -FORM statements.
After the creation of CLass and methods come to SE38 and create the program.
In the program create a object type ref to that class and with the help of that Object call the methods of that Class and display the data.
Example:
REPORT sapmz_hf_alv_grid .
Type pool for icons - used in the toolbar
TYPE-POOLS: icon.
TABLES: zsflight.
To allow the declaration of o_event_receiver before the
lcl_event_receiver class is defined, decale it as deferred in the
start of the program
CLASS lcl_event_receiver DEFINITION DEFERRED.
G L O B A L I N T E R N A L T A B L E S
*DATA: gi_sflight TYPE STANDARD TABLE OF sflight.
To include a traffic light and/or color a line the structure of the
table must include fields for the traffic light and/or the color
TYPES: BEGIN OF st_sflight.
INCLUDE STRUCTURE zsflight.
Field for traffic light
TYPES: traffic_light TYPE c.
Field for line color
types: line_color(4) type c.
TYPES: END OF st_sflight.
TYPES: tt_sflight TYPE STANDARD TABLE OF st_sflight.
DATA: gi_sflight TYPE tt_sflight.
G L O B A L D A T A
DATA: ok_code LIKE sy-ucomm,
Work area for internal table
g_wa_sflight TYPE st_sflight,
ALV control: Layout structure
gs_layout TYPE lvc_s_layo.
Declare reference variables to the ALV grid and the container
DATA:
go_grid TYPE REF TO cl_gui_alv_grid,
go_custom_container TYPE REF TO cl_gui_custom_container,
o_event_receiver TYPE REF TO lcl_event_receiver.
DATA:
Work area for screen 200
g_screen200 LIKE zsflight.
Data for storing information about selected rows in the grid
DATA:
Internal table
gi_index_rows TYPE lvc_t_row,
Information about 1 row
g_selected_row LIKE lvc_s_row.
C L A S S E S
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING
e_object e_interactive,
handle_user_command FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS.
CLASS lcl_event_receiver IMPLEMENTATION
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_toolbar.
Event handler method for event toolbar.
CONSTANTS:
Constants for button type
c_button_normal TYPE i VALUE 0,
c_menu_and_default_button TYPE i VALUE 1,
c_menu TYPE i VALUE 2,
c_separator TYPE i VALUE 3,
c_radio_button TYPE i VALUE 4,
c_checkbox TYPE i VALUE 5,
c_menu_entry TYPE i VALUE 6.
DATA:
ls_toolbar TYPE stb_button.
Append seperator to the normal toolbar
CLEAR ls_toolbar.
MOVE c_separator TO ls_toolbar-butn_type..
APPEND ls_toolbar TO e_object->mt_toolbar.
Append a new button that to the toolbar. Use E_OBJECT of
event toolbar. E_OBJECT is of type CL_ALV_EVENT_TOOLBAR_SET.
This class has one attribute MT_TOOLBAR which is of table type
TTB_BUTTON. The structure is STB_BUTTON
CLEAR ls_toolbar.
MOVE 'CHANGE' TO ls_toolbar-function.
MOVE icon_change TO ls_toolbar-icon.
MOVE 'Change flight' TO ls_toolbar-quickinfo.
MOVE 'Change' TO ls_toolbar-text.
MOVE ' ' TO ls_toolbar-disabled.
APPEND ls_toolbar TO e_object->mt_toolbar.
ENDMETHOD.
METHOD handle_user_command.
Handle own functions defined in the toolbar
CASE e_ucomm.
WHEN 'CHANGE'.
PERFORM change_flight.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMETHOD.
ENDCLASS.
S T A R T - O F - S E L E C T I O N.
START-OF-SELECTION.
SET SCREEN '100'.
*& Module USER_COMMAND_0100 INPUT
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*& Module STATUS_0100 OUTPUT
MODULE status_0100 OUTPUT.
DATA:
For parameter IS_VARIANT that is sued to set up options for storing
the grid layout as a variant in method set_table_for_first_display
l_layout TYPE disvariant,
Utillity field
l_lines TYPE i.
After returning from screen 200 the line that was selected before
going to screen 200, should be selected again. The table gi_index_rows
was the output table from the GET_SELECTED_ROWS method in form
CHANGE_FLIGHT
DESCRIBE TABLE gi_index_rows LINES l_lines.
IF l_lines > 0.
CALL METHOD go_grid->set_selected_rows
EXPORTING
it_index_rows = gi_index_rows.
CALL METHOD cl_gui_cfw=>flush.
REFRESH gi_index_rows.
ENDIF.
Read data and create objects
IF go_custom_container IS INITIAL.
Read data from datbase table
PERFORM get_data.
Create objects for container and ALV grid
CREATE OBJECT go_custom_container
EXPORTING container_name = 'ALV_CONTAINER'.
CREATE OBJECT go_grid
EXPORTING
i_parent = go_custom_container.
Create object for event_receiver class
and set handlers
CREATE OBJECT o_event_receiver.
SET HANDLER o_event_receiver->handle_user_command FOR go_grid.
SET HANDLER o_event_receiver->handle_toolbar FOR go_grid.
Layout (Variant) for ALV grid
l_layout-report = sy-repid. "Layout fo report
Setup the grid layout using a variable of structure lvc_s_layo
Set grid title
gs_layout-grid_title = 'Flights'.
Selection mode - Single row without buttons
(This is the default mode
gs_layout-sel_mode = 'B'.
Name of the exception field (Traffic light field) and the color
field + set the exception and color field of the table
gs_layout-excp_fname = 'TRAFFIC_LIGHT'.
gs_layout-info_fname = 'LINE_COLOR'.
LOOP AT gi_sflight INTO g_wa_sflight.
IF g_wa_sflight-paymentsum < 100000.
Value of traffic light field
g_wa_sflight-traffic_light = '1'.
Value of color field:
C = Color, 6=Color 1=Intesified on, 0: Inverse display off
g_wa_sflight-line_color = 'C610'.
ELSEIF g_wa_sflight-paymentsum => 100000 AND
g_wa_sflight-paymentsum < 1000000.
g_wa_sflight-traffic_light = '2'.
ELSE.
g_wa_sflight-traffic_light = '3'.
ENDIF.
MODIFY gi_sflight FROM g_wa_sflight.
ENDLOOP.
Grid setup for first display
CALL METHOD go_grid->set_table_for_first_display
EXPORTING i_structure_name = 'SFLIGHT'
is_variant = l_layout
i_save = 'A'
is_layout = gs_layout
CHANGING it_outtab = gi_sflight.
*-- End of grid setup -
Raise event toolbar to show the modified toolbar
CALL METHOD go_grid->set_toolbar_interactive.
Set focus to the grid. This is not necessary in this
example as there is only one control on the screen
CALL METHOD cl_gui_control=>set_focus EXPORTING control = go_grid.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*& Module USER_COMMAND_0200 INPUT
MODULE user_command_0200 INPUT.
CASE ok_code.
WHEN 'EXIT200'.
LEAVE TO SCREEN 100.
WHEN'SAVE'.
PERFORM save_changes.
ENDCASE.
ENDMODULE. " USER_COMMAND_0200 INPUT
*& Form get_data
FORM get_data.
Read data from table SFLIGHT
SELECT *
FROM zsflight
INTO TABLE gi_sflight.
ENDFORM. " load_data_into_grid
*& Form change_flight
Reads the contents of the selected row in the grid, ans transfers
the data to screen 200, where it can be changed and saved.
FORM change_flight.
DATA:l_lines TYPE i.
REFRESH gi_index_rows.
CLEAR g_selected_row.
Read index of selected rows
CALL METHOD go_grid->get_selected_rows
IMPORTING
et_index_rows = gi_index_rows.
Check if any row are selected at all. If not
table gi_index_rows will be empty
DESCRIBE TABLE gi_index_rows LINES l_lines.
IF l_lines = 0.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = 'You must choose a line'.
EXIT.
ENDIF.
Read indexes of selected rows. In this example only one
row can be selected as we are using gs_layout-sel_mode = 'B',
so it is only ncessary to read the first entry in
table gi_index_rows
LOOP AT gi_index_rows INTO g_selected_row.
IF sy-tabix = 1.
READ TABLE gi_sflight INDEX g_selected_row-index INTO g_wa_sflight.
ENDIF.
ENDLOOP.
Transfer data from the selected row to screenm 200 and show
screen 200
CLEAR g_screen200.
MOVE-CORRESPONDING g_wa_sflight TO g_screen200.
LEAVE TO SCREEN '200'.
ENDFORM. " change_flight
*& Form save_changes
Changes made in screen 200 are written to the datbase table
zsflight, and to the grid table gi_sflight, and the grid is
updated with method refresh_table_display to display the changes
FORM save_changes.
DATA: l_traffic_light TYPE c.
Update traffic light field
Update database table
MODIFY zsflight FROM g_screen200.
Update grid table , traffic light field and color field.
Note that it is necessary to use structure g_wa_sflight
for the update, as the screen structure does not have a
traffic light field
MOVE-CORRESPONDING g_screen200 TO g_wa_sflight.
IF g_wa_sflight-paymentsum < 100000.
g_wa_sflight-traffic_light = '1'.
C = Color, 6=Color 1=Intesified on, 0: Inverse display off
g_wa_sflight-line_color = 'C610'.
ELSEIF g_wa_sflight-paymentsum => 100000 AND
g_wa_sflight-paymentsum < 1000000.
g_wa_sflight-traffic_light = '2'.
clear g_wa_sflight-line_color.
ELSE.
g_wa_sflight-traffic_light = '3'.
clear g_wa_sflight-line_color.
ENDIF.
MODIFY gi_sflight INDEX g_selected_row-index FROM g_wa_sflight.
Refresh grid
CALL METHOD go_grid->refresh_table_display.
CALL METHOD cl_gui_cfw=>flush.
LEAVE TO SCREEN '100'.
ENDFORM. " save_changes
chk this blog
/people/vijaybabu.dudla/blog/2006/07/21/topofpage-in-alv-using-clguialvgrid
with regards,
Hema.
pls give points if helpful.
Maybe you are looking for
-
How can i find the serial number for an ipod classic
I dont have the original packaging or the reciept, It wont connect to itunes or my laptop and all that shows on the ipod's screen is a red circle with a cross inside it. I have tried to reset it but have not been able to. Any help would be much appre
-
Hi all We have a Plant ,sloc Warehouse set up in SAP,Now we want to extend our warehouse (new building) and define as a new plant ,new Sloc and a new warehouse number.My question is how can i exclude the newly created plant from ATP as we are n
-
A friend gave me his laptop computer. His iTunes account pops up when I click the iTunes icon. I have my own account. How do I make MY account be the one that pops up as default?
-
Unexpected Signal : 11 occurred at PC=0xFECD9110
To. Whom it my concern. I have this error when I install oracle 10gR2 on Solaris 10 (sparc 64bit). I not sure this error ensure from JAVA. Please consult this issue to me. ********************* ERROR *********************************** Unexpected Sig
-
How login in BI Publisher from MyApp (Oracle Forms 11.1.2)
Hi. Anyone can help me, as I login from my application in Oracle Forms 11.1.2, BI Publisher 11.1.6 Thank you very much Carlos