Guys
I am making this simple Addition function in Oracle ADF
In which I am taking three input text fields first two for input numbers and third one for output and a button where i have written code for computing the addition operation.on a page after creating Adf Fusion Application in ADF
This is the code for
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html>
<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
<af:document title="PageAdd.jsf" id="d1">
<af:form id="f1">
<af:inputText label="input1" id="it1" binding="#{Mbean.input1}" autoSubmit="true"/>
<af:inputText label="input2" id="it2" binding="#{Mbean.input2}" autoSubmit="true"/>
<af:inputText label="output" id="it3" binding="#{Mbean.output}" autoSubmit="true"/>
<af:button text="Submit" id="b1" action="#{Mbean.b1_action}"/>
<af:selectBooleanRadio text="selectBooleanRadio 1" label="Label 1" id="sbr1"/>
</af:form>
</af:document>
<!--oracle-jdev-comment:preferred-managed-bean-name:Mbean-->
</f:view>
As you can see the bindings. Mbean is the Managed Bean and the part after '.' is the property.
In the Button I have created this method called b1_action.
Below is the java code.
package view;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import oracle.adf.view.rich.component.rich.input.RichInputText;
public class Addition {
private RichInputText input1;
private RichInputText input2;
private RichInputText output;
public Addition() {
}
public void setInput1(RichInputText input1) {
this.input1 = input1;
}
public RichInputText getInput1() {
return input1;
}
public void setInput2(RichInputText input2) {
this.input2 = input2;
}
public RichInputText getInput2() {
return input2;
}
public void setOutput(RichInputText output) {
this.output = output;
}
public RichInputText getOutput() {
return output;
}
public String b1_action() {
String s;
String x;
String v;
s = (String)input1.getValue();
x = (String)input2.getValue();
int r2=Integer.parseInt(x);
int r1= Integer.parseInt(s);
int d =r2+r1;
v =Integer.toString(d);
output.setValue(v);
System.out.println(output.getValue());
return null;
}
}
While my Application is able to take the values and even add together but not able to display it in the third input text field which i am not able to do
I am new to this Tool and language Java kindly help me.
On your “output” component addd a partial trigger attribute like this:
<af:inputText label="output" id="it3" binding="#{Mbean.output}" autoSubmit=“true" partialTriggers=“ b1"/>
First Make Input 1 and Input 2 autoSubmit="True".
Then Make partialTriggers="it1 it2" for Output.
Make the partialSubmit="True" for the Button.
If nothing happend try to write this.output.setValue(V);
After output.setValue(v);
add this line of code
AdfFacesContext.getCurrentInstance().addPartialTarget(output);
Then set property autoSubmit to “true” inside output in your page
Related
I just complete a Java CONSOLE application for Student Management.
I received a test case set (pdf file contains lines follow according to the requirements of the application) build based on the standard program (from my lecturer). You can overview what my app do and what is format of test casenter image description heree set in the attached image below.
The problem is that I want to use test cases for testing my app but instead of manually entering and matching line by line between Console IO and the pdf file => I want to write a program to automatically import and match the data between my jar/program to test cases.
However, I'm not sure how and where to start.
I have tried with google but unit test/white testing is still the thing that takes up all of my search. Hopefully in the process of continuing to try to search with google, someone will give me some suggestions or directions that will be useful to me. Thanks very much.
[My Program]
[Test cases set]
The way I'd do it is to decouple your application from the console so that you can use fake implementations for printing and reading from the console in your tests. "Fake" is the technical term - you can look up "test doubles" to learn about those and other related ideas. This idea is known as dependency injection, or the dependency inversion principle.
The way we do this is to use interfaces. Here's an example of an application that prints some items:
import java.util.List;
public class ItemPrinterApplication {
public ItemPrinterApplication(OutputWriter outputWriter, List<Item> items) {
this.outputWriter = outputWriter;
this.items = items;
}
public void run() {
outputWriter.writeLine("Name, Price");
items.forEach(item -> outputWriter.writeLine(item.name + ", " + item.price));
}
private OutputWriter outputWriter;
private List<Item> items;
}
OutputWriter is the thing responsible for the printing. It's just an interface, so the application doesn't know whether it writes to the console or somewhere else:
public interface OutputWriter {
void writeLine(String line);
}
For completeness, the Item class just holds some data:
public class Item {
public Item(String name, Integer price) {
this.name = name;
this.price = price;
}
public final String name;
public final Integer price;
}
I can then write a test using JUnit that checks that when I run this application, I get the output that I want. I do that by using an implementation of OutputWriter that just writes to a string. That way it's easy to check in the test:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
public class ItemPrinterTest {
#Test
public void itPrintsAListOfItems() {
List<Item> items =
List.of(
new Item("Apple", 50),
new Item("Carrot", 25),
new Item("Milk", 120)
);
FakeOutputWriter fakeOutputWriter = new FakeOutputWriter();
ItemPrinterApplication app = new ItemPrinterApplication(fakeOutputWriter, items);
app.run();
Assertions.assertEquals(
"Name, Price\n" +
"Apple, 50\n" +
"Carrot, 25\n" +
"Milk, 120\n",
fakeOutputWriter.written
);
}
}
and FakeOutputWriter looks like
public class FakeOutputWriter implements OutputWriter {
public String written = "";
#Override
public void writeLine(String line) {
written += line;
written += "\n";
}
}
This gives me confidence that I'm writing the output correctly. In main, though, I want to actually print to the console:
import java.util.List;
public class Main {
public static void main(String[] args) {
OutputWriter outputWriter = new ConsoleOutputWriter();
List<Item> items =
List.of(
new Item("Apple", 50),
new Item("Carrot", 25),
new Item("Milk", 120)
);
new ItemPrinterApplication(outputWriter, items).run();
}
}
and ConsoleOutputWriter does exactly that:
public class ConsoleOutputWriter implements OutputWriter{
#Override
public void writeLine(String line) {
System.out.println(line);
}
}
You could take the same approach for faking reading input. Your interface would have a function that takes no arguments and reads a string:
interface InputReader {
String readLine()
}
so in the tests you could fake that and in main, read using a Scanner or something.
I'm trying to analyse some bits of Java-code, looking if the code is written too complexly. I start with a String containing the contents of a Java-class.
From there I want to retrieve, given a function-name, the "inner code" by that function. In this example:
public class testClass{
public int testFunction(char x) throws Exception{
if(x=='a'){
return 1;
}else if(x=='{'){
return 2;
}else{
return 3;
}
}
public int testFunctionTwo(int y){
return y;
}
}
I want to get, when I call String code = getcode("testFunction");, that code contains if(x=='a'){ ... return 3; }. I've made the input code extra ugly, to demonstrate some of the problems one might encounter when doing character-by-character-analysis (because of the else if, the curly brackets will no longer match, because of the Exception thrown, the function declaration is not of the form functionName{ //contents }, etc.)
Is there a solid way to get the contents of testFunction, or should I implement all problems described manually?
You need to a java parser. I worked too with QDox. it is easy to use. example here:
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import java.io.File;
import java.io.IOException;
public class Parser {
public void parseFile() throws IOException {
File file = new File("/path/to/testClass.java");
JavaProjectBuilder builder = new JavaProjectBuilder();
builder.addSource(file);
for (JavaClass javaClass : builder.getClasses()) {
if (javaClass.getName().equals("testClass")) {
for (JavaMethod javaMethod : javaClass.getMethods()) {
if (javaMethod.getName().equals("testMethod")) {
System.out.println(javaMethod.getSourceCode());
}
}
}
}
}
}
Have you considered using a parser to read your code? There are a lot of parsers out there, the last time I worked on a problem like this http://qdox.codehaus.org made short work of these kinds of problems.
I want to use JSoup for a very simple purpose: to strip character codes from some snippets of HTML text. ExStrip holds three strings, which are meant to get parsed if passed to constructor or to set methods. The import is recognised in the constructor, but not in the subsequent method:
import org.jsoup.*;
public class ExStrip {
private String catalogue;
private String title;
private String fulltext;
public ExStrip(String sColl, String sTit, String sFull) {
catalogue = Jsoup.parse(sColl).text();
title = Jsoup.parse(sTit).text();
fulltext = Jsoup.parse(sFull).text();
// works fine, JSoup recognised
}
public void setCatalogue(String coll) {
this.catalogue = JSoup.parse(coll).text();
// cannot find symbol, symbol: variable JSoup
}
public void setTitle(String coll) {
this.title = JSoup.parse(coll).text();
// cannot find symbol, symbol: variable JSoup
}
public void setFull(String coll) {
fulltext=coll;
}
public String getCatalogue() {
return catalogue;
}
public String getTitle() {
return title;
}
public String getFull() {
return fulltext;
}
}
I'm doing this in NetBeans. The jsoup jar file is imported properly, I think, in the project properties, and it does show up in the project. I also tried importing the JSoup libraries more precisely than a star import, which didn't help, and in any case, why would an exact same call work in one method of a class and not another?
I'd appreciate any help with this.
You state:
JSoup import not recognised in a method
Your imports are actually being recognized just fine, but you need to remember that for Java, both spelling and capitalization are important.
JSoup != Jsoup
So change:
this.catalogue = JSoup.parse(coll).text();
to:
this.catalogue = Jsoup.parse(coll).text();
and make similar changes throughout your program.
I'm currently working on some project, using Apache Tapestry 5.3.6. I have issue using t:loop component. Is there any way I can get selected item after loop finishes, and page is rendered?
What I need to achieve is: Let's say I have loop like this:
<t:loop t:source="itemList" t:value="item">
<t:actionlink id="something" context="item.ID"></t:actionlink>
</t:loop>
This will work fine. But if I move this actionlink into my own component, and pass this ID through my parameter, if I click, I always get the last item from list, and not the one which is clicked.
<t:loop t:source="itemList" t:value="item">
<t:mycomponent myparameter="item.ID"></t:mycomponent>
</t:loop>
I tried putting formState="iteration", and puting ValueEncoder, but nothing helps.
Please, can anyone help me, and show me how to solve this issue, and get the selected item from the list.
Thanks in advance
Edit: Here is code of my component
public class Ocenjivanje
{
#Parameter(required=true)
#Property
private int materijalID;
private Materijal materijal;
#Inject
private Session session;
#SessionState
private User user;
#CommitAfter
public Object unesiOcenu(int ocena)
{
Materijal m = (Materijal)session.createCriteria(Materijal.class).add(Restrictions.eq("materijalID", this.materijalID)).list().get(0);
Date d = new Date();
Ocena o = new Ocena();
o.setMaterijal(m);
o.setKorisnikID(this.user.getID());
o.setDatumOcene(d);
o.setOcena(ocena);
session.save(o);
return this;
}
public void onActionFromJedan()
{
unesiOcenu(1);
}
public void onActionFromDva()
{
unesiOcenu(2);
}
public void onActionFromTri()
{
unesiOcenu(3);
}
public void onActionFromCetiri()
{
unesiOcenu(4);
}
public void onActionFromPet()
{
unesiOcenu(5);
}
}
<t:container
xmlns="http://www.w3.org/1999/xhtml"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">
Oceni sadržaj:
<t:actionlink t:id="jedan">1</t:actionlink>
<t:actionlink t:id="dva">2</t:actionlink>
<t:actionlink t:id="tri">3</t:actionlink>
<t:actionlink t:id="cetiri">4</t:actionlink>
<t:actionlink t:id="pet">5</t:actionlink>
I'm not quite sure what you are trying to achieve but in any case you do not use the context you pass in in your actionlinks and use hardcoded int's in stead. Change your action links to <t:actionlink t:id="tri" context="materijalID">3</t:actionlink> and change your event handlers to
public void onActionFromJedan(int context)
{
unesiOcenu(context);
}
I am attempting to create a component that if given the following tml:
<t:slideout>
<p:header>Short Description of Data</p:header>
Long Details about the data here
</t:slideout>
This should initially render the block in the header parameter, when this block is clicked I want the long details section to slide out using the jQuery .slideDown() function or equivalent.
Currently I have the following class:
public class slideout
{
#Parameter(name="header", defaultPrefix = BindingConstants.LITERAL)
private Block headerBlock;
public Block getHeader()
{
return headerBlock;
}
}
and the corresponding slideout.tml file:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">
<body>
<t:delegate to="header"/>
<t:body/>
</body>
</html>
We are already making use of the tapestry5-jQuery library, this component needs be able to be used multiple times on the same page so I'm also not sure of how to ensure that there are no ID collisions when rendering the page.
I'm not sure where to progress from here, if I was doing this in raw HTML/jQuery I'd do something like
$('slideout-header').click(function() {
$(this).next('slideout-body').slideDown();
});
However I'm not sure of what the "Tapestry" way of constructing these classes would be. What is the best way to solve this problem in Tapestry 5?
You can add a Slideout.js file next to your Slideout.tml:
Tapestry.Initializer.Slideout = function (parameters) {
var yourClientId = parameters.clientId;
//your javascript init script here
};
Add to your Slideout.java:
#Import(library = {"Slidout.js"})
public class Slideout {
#Inject
private JavaScriptSupport javaScriptSupport;
#Parameter(name="header", defaultPrefix = BindingConstants.LITERAL)
private Block headerBlock;
#Property
#Parameter(value = "prop:componentResources.id", defaultPrefix = "literal")
private String clientId;
#AfterRender
private void afterRender() {
JSONObject props = new JSONObject();
props.put("clientId", clientId);
javaScriptSupport.addInitializerCall("Slideout", props);
}
public Block getHeader()
{
return headerBlock;
}
}
and your Slideout.tml (note that I removed the html so that you can use Slideout as a component)
<div id="${clientId}" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">
<t:delegate to="header"/>
<t:body/>
</div>
Disclaimer: I have not tested this code so have a play.