I am very new to java wicket. I don't understand how to run this code. My wicket program follows the structure below. My problem is that I am not able to run this. I am getting a 404 error.
This is my wicket code to get a hello world message.
HelloWorld.html:
<html xmlns:wicket>
<title>Hello World</title>
</head>
<body>
<span wicket:id="message" id="message">Message goes here</span>
</body>
</html>
</html>
HelloWorld.java:
package com.sensiple.wicket;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
public class HelloWorld extends WebPage {
public HelloWorld() {
add(new Label("message", "Hello World!"));
}
}
This class is returning hello world which is to be printed in HelloWorld.html
HelloWorldApplication.java:
package com.sensiple.wicket;
import org.apache.wicket.protocol.http.WebApplication;
public class HelloWorldApplication extends WebApplication {
//what is the need of this constructor, need of this class in this program
public HelloWorldApplication() {
}
I need to know what is the use of getHomePage-method too, as I am not getting what is the use of one more class here which returns return type as HelloWorld. Hardly I am not able to run this code. I went through many resources which didn't help.
public Class<HelloWorld> getHomePage() {
System.out.println("initialized!!!!");
return HelloWorld.class;
}
}
To start answering your many questions:
Your program is most likely returning a 404 due to the malformed HTML in your HelloWord.html. Wicket required valis XHTML to work with.
The original signature of getHomePage() is public abstract Class<? extends Page> getHomePage(). You can implement it as you did as it fullfills the contract. The method returns the class that is used to render the homepage of your web application. That's the page that is shown at the base-url of your application without any mountpoints or parameters.
You don't need the default constructor to your WebApplication but you can implement one to do some of the initialisations needed for your application. It's run once at the start of your application (or in most cases your container).
Try using this html:
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<title>Hello World</title>
</head>
<body>
<span wicket:id="message"></span>
</body>
</html>
When your application is running, just call http://localhost:8080 (unless you changed the port) and wicket should redirect you to your HelloWorld page
Related
i have created a basic dynamic web project in eclipse.
my jsp file contains following code
<html>
<head>
<title>Insert title here</title>
</head>
<body>
this is the sum of 5 and 3 <%com.testing.SumOfNumbers.addTwoNumbers(3,5); %>
</body>
</html>
and in package "com.testing" i have created SumOfNumbers class with addTwoNumbers method.
package com.testing;
public class SumOfNumbers {
public static int addTwoNumbers(int a,int b)
{
return a+b;
}
}
i have downloaded tomcat v10.0 server.
I tried several times but same result. I don't know where the problem is ,is it in tomcat server or eclipse bug?
thanks in advance.
It's unlikely to be your code, although you're using a scriptlet instead of an expression, so the sum won't be in the output like you seem to expect.
https://wiki.eclipse.org/How_to_report_a_deadlock includes directions on how to get a stack dump to see what's going on.
I wonder if it is possible to modify the HTML code of the parent of a wicket component in the Java code to modify its attribute without making it a component in wicket. For example, I would like to add active to the li tag from Java.
<li>
<a wicket:id="home" href="#">
<i class="icon-home"></i>
<span>Home</span>
</a>
</li>
and the say add an Attribute to that parent without referencing it in code as a component.
I don't think this is possible, it would be completely against the modular nature of Wicket. (Not to mention the fact that pages are actually rendered as a stream, there's no DOM tree built.)
Components in Wicket shouldn't depend on what's outside of them. What if you want to change the logic of active/inactive controls? Or, in a more likely scenario, you just want to change the markup. Or if you just want to unit test your component without any surrounding markup.
Wicket was designed to avoid these "spooky actions at a distance", to create components that are genuinely testable on their own.
You need a component that encapsulates the entire list, which keeps track of which of its items is active (via its model). It might seem at first like a lot of work but when you look at the result, you'll realise how much easier it is to understand what's going on.
You can do it using javascript. In this example I overrided the renderHead() of the link, but it can also be done with a Behavior.
public class TestPage extends WebPage {
public TestPage(final PageParameters parameters) {
super(parameters);
add(new AjaxLink<Void>("link") {
boolean active = false;
#Override
public void onClick(AjaxRequestTarget target) {
active = !active;
target.add(this);
}
#Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
String addOrRemove = (active) ? ".addClass('active')" : ".removeClass('active')";
response.render(OnDomReadyHeaderItem.forScript("$('#" + getMarkupId() + "').parent('li')" + addOrRemove + ";"));
}
});
}
}
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<style type="text/css">
li.active {background-color: red;}
</style>
</head>
<body>
<ul>
<li><a wicket:id="link">TOGGLE</a></li>
</ul>
</body>
</html>
Yes, it will couple the code to the markup, but this is not always a problem. If you feel you are copying and pasting this code over and over, consider creating a proper component :)
Does anybody know what's wrong with this code? It shows nothing in the browser.
My intention is to draw two eyes based on the mouse location. So I define an Eye class and then call its draw method with variable mouseX and mouseY.
However, it shows nothing on the browser.
Also, I wonder how to detect the size of the browser because screen.width and screen.height don't seem to work within a browser.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="processing.js"></script>
<script type="text/processing" data-processing-target="mycanvas">
void setup(){
size(400,400);
smooth();
background(225);
}
if (mousePressed){
Face.draw(mouseX,mouseY);}
class Face{
float x,y,a;
Face(float ax,float ay){
x=ax;
y=ay;
a=random(1,5);}
void draw{
noFill();
stroke(0);
//eye1
ellipse(x+2.2*a,y-a,a/2,a/2);
//eyelashes1
arc(x+2.2*a,y-.5*a,1.2*a,1.2*a,PI,2*PI);
//eyebrows1
arc(x+3*a,y-.3,2*a,2*a,5/4*PI,9/4*PI);
//eye2
ellipse(x-2.2*a,y-a,a/2,a/2);
//eyelashes2
arc(x-2.2*a,y-.5*a,1.2*a,1.2*a,PI,2*PI);
//eyebrows2
arc(x-3*a,y-.3,2*a,2*a,5/4*PI,9/4*PI);
}
}
</script>
</head>
<body></body>
<canvas id="mycanvas"></canvas>
</html>
I'm a beginner, so I don't know if my problem is stupid or not.
But any hint is welcomed:)
Btw how to add color to the code on stackoverflow?
There are a some problems with your script.
Related with the html page:
You have to declare the encoding of the document in the header section: <meta charset="utf-8">
The canvas element should be included inside the html body.
Related with the processing code:
The processing script needs at least one setup() function and one draw() function. You defined a draw() method in your Face class, but not the main draw() function (by the way, you missed the brackets in your draw() method).
The mousePressed functionality should be included inside the main draw() method.
You need at least to declare one instance of your Face class (inside the setup() function). For example: Face myFace = new Face(10, 10);.
And probably more syntax errors inside your draw() method...
My recommendations:
Start with something simpler: The more lines you write from scratch, the harder to debug. Try to draw first just an ellipse and incrementally add more things to your code.
Keep your processing script in a separate file. It's cleaner and you can debug it using the processing editor or the processing online sketch:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Testing testing</title>
<script type="text/javascript" src="processing.js"></script>
</head>
<body>
<canvas id="my-sketch" data-processing-sources="my-sketch.pde"></canvas>
</body>
</html>
Check this tutorial to get some ideas about objects and classes in processing.
The syntax in processing is very similar to java, and there're a lot more resources for understanding the basics of OOP for that language. Just google for it.
For syntax highlightning, look here.
How do you edit the HTML tag in a Kentico CMS Masterpage, i.e. to add a class attribute?
Currently I have this:
But I want:
<!DOCTYPE html>
<html class="myClass">
<head>
<title>
From the screenshot you can see that the HTML tag is not editable.
Is there a way to edit the html tag or tell Kentico to write a class attribute to the html tag?
I have found one solution:
I manually added a class attribute to the HTML tag in this file:-
<solution>/CMSPages/PortalTemplate.aspx
You can also use the "Head HTML" web part on your page template(s).
It is quite disappointing that Kentico still does not support this out of the box. A request to be able to modify the <html> tag directly from code has been filed: http://ideas.kentico.com/forums/239189-kentico-product-ideas/suggestions/5947264-ability-to-modify-page-head-section-and-html-tag
In the meantime use the following solutions:
Portal Engine
I solved this the following way, based on Dave Haigh suggestion. In the file /CMSPages/PortalTemplate.aspx change the following line:
<html xmlns="http://www.w3.org/1999/xhtml" <%=XmlNamespace%>
lang="<%=CMSContext.PreferredCultureCode%>">
ASPX Engine
In your master page, add the following snippet to your code behind file:
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (CurrentDocument != null)
{
CMS.UIControls.ContentPage page= this.Page as CMS.UIControls.ContentPage;
if (page != null)
{
string lang= CMS.Localization.LocalizationContext.CurrentCulture.CultureCode;
page.XmlNamespace += " lang=\"" + lang + "\"";
}
}
}
</script>
Source: http://devnet.kentico.com/questions/kentico-9-how-to-define-language-in-html
I got a page with several panels that takes several parameters in their constructors.
One of them being a menu that takes a list for the different buttons in the menu.
I've tried to test it as components but get a null pointer exception. Using a dummy page and creating the panel on the dummy page works.
I'm not entirely happy with this approach since it introduces a lot of new code in my tests and more possibilities for errors.
Is there a better way of testing panels that takes arguments in their constructor?
Sure thing:
The code that gives an null pointer error:
public void testVisitPanel(){
VisitPanel v = new VisitPanel("visitPanel");
tester.startComponent(v);
tester.assertContains("DATE");
}
The panel
public VisitPanel(String id) {
super(id);
add( new Label("visitDate", "DATE"));
add( new Label("visitStage", "VISIT SIGNED"));
}
And the html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.sourceforge.net/"
xml:lang="en"
lang="en">
<wicket:head>
</wicket:head>
<body>
<wicket:panel>
<span wicket:id="visitDate">VISIT DATE</span>
<span wicket:id="visitStage">STAGE</span>
</wicket:panel>
</body>
</html>
If you're using Wicket 1.4, you probably want wicketTester.startPanel.
What I do is create an implementation of ITestPanelSource in the test, doing something like:
private class TestPanelSource implements ITestPanelSource {
private static final long serialVersionUID = 1L;
public Panel getTestPanel(String panelId) {
return new MyPanel(panelId, myArg1, myArg2);
}
}
with the myArgN being fields in the test class (frequently mocks) that suit the constructor, and then call it in the test or in a setUp method with
wicketTester.startPanel(new TestPanelSource());
This is basically doing some of the DummyPage work for you, so it may not be that far from what you're doing now, but might at least save on implementation of dummy pages for test infrastructure.
In Wicket 1.5, this is deprecated in favor the component testing that you referenced in the question. That should also work, so it might be worthwhile to post some actual code that is giving you trouble with that technique.