I'm coding a program in java that take a few information from a graph (X axis and Y axis), and then I need to take this information and pass to a JSP page.
this is the java
package view;
public class Axis{
double[] axisX;
double[] axisY;
int test;
public Axis(){
}
public void setAxisX(double[] axisX){
this.axisX = axisX;
}
public void setAxisY(double[] axisY){
this.axisY = axisY;
}
public double[] getAxisX(){
return axisX;
}
public double[] getAxisY(){
return axisY;
}
}
Then in the JSP is this what I have to do?
<jsp:useBean id="view" class="view.Axis" scope="session"/>
Test: <%= view.getAxisX() %>
What you are basically trying to do is use Standard Actions :
<jsp:useBean id="view" class="view.Axis" scope="session"/>
<br> X :<jsp:getProperty name="view" property="axisX" />
<br> Y :<jsp:getProperty name="view" property="axisY" />
You can use EL/JSTL for this instead of Standard Actions.
Also read ,
How to avoid Java Code in JSP-Files?
Related
I'm trying to request two numbers (first and second) as input via a form using Thymeleaf, and have the numbers added and the answer returned. The code is run from:
http://localhost:8080/add
I've based the program on this link:
https://spring.io/guides/gs/validating-form-input/
I'm also using the latest version of Springboot, currently 1.5.8.
I'm finding this simple task becoming somewhat convoluted (compared to say python), with continual errors that aren't making any sense. My current problem is I'm getting the following error, which even after checking similar posts online, I can't resolve...
Error:
"Exception processing template "add": Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'"
Based on these posts I've change "long" to "Long" in my NumberForm, and moved my NumberForm.java under "sec.calculator", but no joy...
Any help would be appreciated !
Here's my code:
1) src/main/java
CalculatorController.java:
package sec.calculator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
#Controller
public class CalculatorController {
#GetMapping("/add")
public String addForm(Model model) {
model.addAttribute("add", new NumberForm());
return "add";
}
#PostMapping("/add")
public String addSubmit(#ModelAttribute NumberForm add) {
return "result";
}
}
------------------------------------
CalculatorApplication.java
package sec.calculator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CalculatorApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(CalculatorApplication.class, args);
}
}
------------------------------------
NumberForm.java
package sec.calculator;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**import javax.validation.constraints.Size;
*/
public class NumberForm {
#NotNull
#Min(0)
private Long first;
private Long second;
public Long add_out;
public Long add_output() {
return add_out ;
}
public void Setfirst(Long first) {
this.first = first;
}
public void Setsecond(Long second) {
this.second = second;
}
}
-----------------------------------
2) src/main/resources/templates
add.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project
Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Form</h1>
<form action="#" th:action="#{/add}" th:object="${NumberForm}" method="post">
<p>First: <input type="text" th:field="*{First}" /></p>
<p>Second: <input type="text" th:field="*{Second}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
------------------------------------------
Welcome to SO.
There are several issues here. In particular, Java is very much case-sensitive.
Follow the convention for setters so that Thymeleaf can
understand it:
public void setFirst(Long first) {
this.first = first;
}
public void setSecond(Long second) {
this.second = second;
}
I am assuming you left out the analogous getter methods.
Even better, see Project Lombok - stop the madness entirely and use
#Setter and #Getter or #Data.
In your form, lower-case your field names:
<p>First: <input type="text" th:field="*{first}" /></p>
<p>Second: <input type="text" th:field="*{second}" /></p>
I'd also stay away from variable names like these since they can become very confusing.
Also in your form, you are using th:object="${NumberForm}" but
you named your variable add. Thymeleaf knows nothing about this variable named NumberForm. Your variable is named add of a type NumberForm, but Thymeleaf will need the name of the variable here.
So you can do: model.addAttribute("numberForm", new
NumberForm());
and th:object="${numberForm}"
Using lower-case for your th:object (modelAttribute) is by
convention.
You'll actually need to include some code to sum the values. I'll
leave that part for you.
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
I need to do the following:
User logs in.
Redirected to welcome screen.
Looks at the welcome screen while lots of records are loaded.
Redirected to the working screen.
I am looking for a way to do in Action class something like this:
public class LinkAction extends ActionSupport implements SessionAware {
#Autowired
private ServiceDelegate myService;
public String welcome()
{
new Runnable() {
#Override
public void run() {
myService.getLoadsOfData();
//redirect to the next action
}
}.run();
// this is where the user
// goes to look at the welcome screen
return "welcome";
}
}
May be it's a wrong approach, please tell if so, I am new at Struts.
The right way is the one already suggested by AleksandrM in comments: open the page, show an indicator while you call an ajax action (let's say with jQuery, for convenience), then render the result and remove the indicator. It's easier than you think:
public class MainAction extends ActionSupport {
public String execute() {
return SUCCESS;
}
}
public class AjaxAction extends ActionSupport {
#Autowired
private ServiceDelegate myService;
private Stuff myStuff; // with getter
public String execute() {
myStuff = myService.loadLongStuff();
return SUCCESS;
}
}
Your AJAX action can either return JSON data, a JSP snippet or a Stream of binary data. Choose the way you prefer. For example, if you map SUCCESS of AjaxAction to a JSP snippet, your JSP snippet will be:
ajaxSnippet.jsp
<%# taglib prefix="s" uri="/WEB-INF/struts-tags.tld" %>
Stuff: <s:property value="myStuff" />
Then in your main.jsp, show the indicator in the div you will overwrite with the AJAX call's result:
main.jsp
<body>
<div id="main">
<img src="/images/mesmerizingProgressBar.gif" />
</div>
<script>
$(function(){ // onDocumentReady...
$.ajax({ // call ajax action...
type : 'GET',
url : '/ajaxAction.action',
success : function(data,textStatus,jqXHR){
// then render your result in "main" div,
// overwriting the loading GIF
$("#main").html(data);
},
error : function(jqXHR, textStatus, errorThrown){
$("#main").html("Error ! " + textStatus);
}
});
});
</script>
</body>
Thank you for the AJAX idea.
However, the answer I was looking for was in fact Struts interceptor "execAndWait".
I decided to use it over AJAX because I am dealing with existing application and all the Struts plumbing is in place.
This is the Struts guide on this
Alert message is not displaying when i click button. if i click a button it should show alert message as success with 3 minutes delay.
When i click the button, first it is not responding second time only it getting the value from the bean.
Why what is the reason it is not loading for the first time?
xhtml code :
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
<script>
var myVar;
function myFunction() {
myVar = setTimeout(function(){message();},3000);
}
function message(){
myVar=setTimeout(function() {
if(#{mytest.count}===0) {
alert("Fails");
}
else {
alert("Success");
}
},3000);
}
</script>
</h:head>
<h:body>
<h:form>
<h:commandButton onclick="message();" action="#{mytest.func}" value="click"/>
</h:form>
</h:body>
</html>
Managed Bean Code :
package com.display;
import javax.faces.bean.ManagedBean;
#ManagedBean
public class Mytest {
private int count=0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void func(){
this.count=1;
System.out.println("function called");
System.out.println("count value is -->"+count);
}
}
Couple of thing I wanted to point out. (This might not be an Answer but I wanted to raise some refactoring stuff which could help you resolve the issue.)
1. if i click a button it should show alert message as success with 3 minutes delay.
But as per your code it is:
myVar = setTimeout(function(){message();},3000); //3 seconds (1000 ms = 1 seconds)
2. myvar is defined globally which is assigned to both function myFunction() and message. I don't see any use of myvar in your code.
Edit
Basically, your javascript code is getting invoked before going to your bean class. Check whether you can have any method oncomplete in your commandbutton.
Alternatively you can wrap <f:ajax> in your <h:commandButton> like,
<f:ajax onevent=myFunction();/>
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.