Value is not posting in a .jsp page - forms

This is the code that I have for the page that I'm testing out:
Don't worry the startpage and endpage code works.
<% startpage(out); %>
<%
String newusers = request.getParameter("users");
int i = 0;
if (newusers != null){
while(i<10)
{
out.println("<h2>newusers</h2>");
i++;
}
}
%>
<form action="guestAdder.jsp" method=post>
<textarea name=users rows=20 cols=100>
</textarea>
<p>
<input type=submit name=doadd value="Add Users">
</form>
<% endpage(out); %>
it is a HTTP Status 500 - java.lang.NoClassDefFoundError: org/apache/jsp/guestAdder_jsp (wrong name: org/apache/jsp/guestadder_jsp)

I'm guessing you've renamed your jsp file from guestadder.jsp to guestAdder.jsp. Are you on Windows (which isn't case sensitive for filenames)? Looks like the servlet class generated from the JSP page is overwriting the old lowercase-named file but the file isn't being renamed, i.e it is keeping its old name of guestadder_jsp.class
Clear the directory used to cache the servlet classes generated from JSPs - in Tomcat this is tomcat/work

Related

OpenXava: Retain URL after load image

The default behaviour for uploading image, using Stereotype("PHOTO") will change the url. For example, localhost:8080/m/Activity?Uid=15&Year=2017 will change to localhost:8080/m/Activity?application=Survey&module=Activity once image loaded. I have tried to retain the url with the code shown below but it does not work. Can somebody help me on how to retain the previous url?
ImageEditor.jsp
<%# include file="../imports.jsp"%>
<%# page import="org.openxava.model.meta.MetaProperty" %>
<%# page import="org.openxava.web.Ids" %>
<%
String propertyKey = request.getParameter("propertyKey");
MetaProperty p = (MetaProperty) request.getAttribute(propertyKey);
boolean editable="true".equals(request.getParameter("editable"));
String uId = request.getParameter("UId");
String year = request.getParameter("Year");
long dif=System.currentTimeMillis(); // to avoid browser caching
%>
<img id='<%=propertyKey%>' name='<%=propertyKey%>' src='<%=request.getContextPath()%>/xava/ximage?UId=<%=uId%>&Year=<%=year%>&property=<%=propertyKey%>&dif=<%=dif%>' title="<%=p.getDescription(request)%>" alt=""/>
<% if (editable) { %>
<span valign='middle'>
<xava:link action='ImageEditor.changeImage' argv='<%="newImageProperty="+Ids.undecorate(propertyKey)%>'/>
<xava:action action='ImageEditor.deleteImage' argv='<%="newImageProperty="+Ids.undecorate(propertyKey)%>'/>
</span>
<% } %>
Thanks.
Brian
OpenXava changes the URL after uploading, it works in that way. You should store the parameter values in a session object to not lossing them.

How to call Java Class from JSP page? Cloud9 & OpenShift JBoss

I am new to c9. I created a Openshift JBoss application server & imported into C9 via GitHub.
I have a simple Java class in Main\Java\initial.class.
public class initial {
public static void main(String[] args) {
String prn = disp();
System.out.println(prn);
}
public static String disp()
{
String str=" JAVARANCH ";
return str;
}
}
I have a jsp page called from index.html, I am just trying to display the variable 'prn' in the jsp page.
<HEAD>
<TITLE>JBossAS7 JSP snoop page</TITLE>
<%# page import="javax.servlet.http.HttpUtils,java.util.Enumeration" %>
<%# page import="java.lang.management.*" %>
<%# page import="java.util.*" %>
<%# page import="initial" %>
</HEAD>
<BODY>
<H1>WebApp JSP Snoop page</H1>
<img src="images/jbosscorp_logo.png">
<h2>JVM Memory Monitor</h2>
<input type="text" name="text1" size="100" value="<%=prn%>">
But, I am getting below error at the JSP page:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 10 in the generated java file
The import initial cannot be resolved
An error occurred at line: 15 in the jsp file: /snoop.jsp
prn cannot be resolved to a variable
12: <img src="images/jbosscorp_logo.png">
13:
14: <h2>JVM Memory Monitor</h2>
15: <input type="text" name="text1" size="100" value="<%=prn%>">
16:
17: <table border="0" width="100%">
18:
How do I interact with a Java class from my JSP?
thanks.
EDIT due to two answers:
I think the main issue is: the class 'initial' is not being imported successfully. My call to Prn was wrong, I understand that now, but the Import itself is not working.
some suggested having the class inside a package. But Cloud9 IDE does not have an option to create a package.
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 10 in the generated java file
The import initial cannot be resolved
If you try this :
Java class:
public class Initial {
public static String getDisp() {
return "JAVARANCH";
}
}
JSP file :
<HEAD>
<TITLE>JBossAS7 JSP snoop page</TITLE>
<%# page import="javax.servlet.http.HttpUtils,java.util.Enumeration" %>
<%# page import="java.lang.management.*" %>
<%# page import="java.util.*" %>
<%# page import="Initial" %>
</HEAD>
<BODY>
<H1>WebApp JSP Snoop page</H1>
<img src="images/jbosscorp_logo.png">
<h2>JVM Memory Monitor</h2>
<input type="text" name="text1" size="100" value="<%=Initial.getDisp()%>">
</BODY>
You can access your class from your jsp by adding the following.
<input type="text" name="text1" size="100" value="<%= initial.disp() %>">
or by adding a jsp scriptlet prior to your input
<%
String prn = initial.disp();
%>
<input type="text" name="text1" size="100" value="<%= prn %>">
Notice I am using your method disp() not main(). Main is static void with no return.

RequiredPageContentNotPresent error in geb 0.12.2

I am getting the following error when trying to run a geb test as a part of a Maven build in Eclipse:
Scenario: Perform login Time elapsed: 0.009 sec <<< FAILURE!
geb.error.RequiredPageContentNotPresent: The required page content 'userName - SimplePageContent (owner: LoginPage, args: [], value: null)' is not present
The only pattern to the error seems to be that it is thrown every time I try to use the geb selector to select/find some page content, but using the selector should work out-of-the-box, right?
I am using the following tools and libraries:
Eclipse Mars Java EE ID (version: 4.5.1)
geb (version 0.12.2)
Selenium (version 2.48.2)
Groovy (version 2.4.5)
Cucumber (version 1.2.4)
Here is my .feature file:
Feature: Login
Scenario: Perform login
Given the user is at the login page
When the user enters <some_uid> and <some_pwd>
Then the user should be logged in
Here is my steps definition:
package stepdefs
import pages.LoginPage
import static cucumber.api.groovy.EN.*
Given(~"the user is at the login page") { ->
to LoginPage
assert at(LoginPage)
}
When(~"the user enters (.*) (.*)") { user, password ->
at LoginPage
page.doLogin(user,password)
}
Then(~"the user should be logged in"){ ->
assert at(LoginResultPage)
}
Here is my page definition:
package pages
import geb.Page
class LoginPage extends Page {
static url = "/TSADG_BORGER/loginpin.do"
static at = { title == "TastSelv Borger" }
static content = {
loginForm { $($/form/$,id:"mainForm") }
userName { loginForm.find("input",id:"pnr") }
pass { loginForm.find("input",id:"tastselvKode") }
buttonLogin { loginForm.find("input",id:"bt1") }
}
def doLogin(user, password) {
userName = user
pass = password
buttonLogin.click()
}
}
Here is the HTML for the form:
<form id="mainForm" action="/TSADG_BORGER/loginpin.do" role="form" method="post" autocomplete="off">
<input type="hidden" name="dispatch" value="Valider">
<div class="row skts-centered-padding">
<div class="col-sm-12">
<h1>Log på med TastSelv-kode</h1>
</div>
<div class="col-sm-6">
<div class="skts-process-form-section skts-required ">
<p><label for="pnr">Cpr-nummer</label></p>
<div>
<input id="pnr" type="text" value="" class="form-control skts-required-val" name="pnr" size="16" maxlength="14" data-validation-event="blur" data-show-type="string" data-show-facets="pattern" data-show-facet-values="/(^\d{10}$)|(^\d{6}\-\d{4}$)/" aria-required="true" aria-invalid="false" aria-describedby="pnrError " autocomplete="off">
</div>
</div>
<div class="skts-process-form-section skts-required">
<p><label for="tastselvKode">TastSelv-kode</label></p>
<div>
<input id="tastselvKode" type="password" value="" class="form-control skts-required-val" name="tastselvKode" size="16" maxlength="16" data-show-type="string" data-show-facets="pattern" data-show-facet-values="/^[^]{7,16}$/" data-validation-event="blur" aria-required="true" aria-invalid="false" aria-describedby="tastSelvKodeError " autocomplete="off">
</div>
</div>
<br>
<p>
<input type="submit" id="bt1" class="btn btn-primary skts-validate" value="Fortsæt" autocomplete="off">
</p>
</div>
</div>
</form>
Any help and input is appreciated.
I would rearrange your content section a bit:
static content = {
loginForm { $("form#mainForm"") }
userName { $("input#pnr") }
pass { $("input#tastselvKode") }
buttonLogin { $("input#bt1") }
}
Use jQuery selectors in Geb! They are very handy. Note that if you use unique ids $("#mainForm"") will have the same effect as $("form#mainForm""). First version is easier, second is more expressive ;)
More info: http://www.w3schools.com/jquery/jquery_ref_selectors.asp
And: http://www.gebish.org/manual/current/#the-jquery-ish-navigator-api
The problem has been resolved.
I was so convinced that it had to be a problem with the selectors, so it took me a while to get my head out of the sand and take a step back. When I did that, I realised that the URL in the "LoginPage" class actually did a redirect to another page, which I was not expecting. The page I was redirected to did obviously not contain any of the objects I was looking for in my contents section. So, when the error message stated that "The required page content...is not present", then that was absolutely correct.
Really silly mistake on my part...

Insert Image to a MVC Project using jQuery

I want to insert an imege to a MVC Project, i used following code to do that,
but i want to pass that image using jquery instead of pass data using Html.BeginForm.
how can i do it,
<% using (Html.BeginForm("uploadImage", "ItemMaster", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
%>
<p><input type="file" id="fileUpload" name="fileUpload" size="23"/> </p>
<p><input type="submit" value="Upload file" /></p>
<%= Html.Hidden("hdnItemCode", null, new { style = "width:100px" })%>
<%
}
%>
If you're looking for a method to upload a file via jQuery, I suggest this question and answers.

ASP.NET MVC2 +file uploading (HttpPostedFileBase class)

I have problem with uploading my file. I want to upload it from my edit view:
<%
using (Html.BeginForm("edit","profile",FormMethod.Post, new { enctype="multipart/form-data" }))
{%>
<%: Html.ValidationSummary(true) %>
<%: ViewData["ErrorMessage"] %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Image) %>
</div>
<div class="editor-field">
<input type="file" id="Image" name="Image" />
<label id="LabelErrorImage" class="errorMessage" />
</div>
<p>
<input type="submit" value="Save" onclick="return Validate(); return false;"/>
</p>
</fieldset>
<% } %>
I want to use HttpPostedFileBase class. My edit action:
[Authorize]
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(string id, HttpPostedFileBase file, FormCollection formValues)
{
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
CustomHelpers.createFolder();
var tmpPath = MyConfig.UPLOAD_FILE_PATH + "/" + Membership.GetUser().ProviderUserKey.ToString();
var path = Path.Combine(Server.MapPath(MyConfig.UPLOAD_FILE_PATH), "Avatar");
var fileExtension = Path.GetExtension(file.FileName);
file.SaveAs(path);
user.Image = "Avatar";
}
adventureDB.SaveChanges();
return RedirectToAction("Index");
}
}
But I always have empty the file object, why????? Do you have any ideas, suggestions why it can work like that? Maybe there is problem how I pass on the file value to my Edit action?
EDIT:
IT IS REALLY STRANGE AS EVEN WHEN I REMOVE
using (Html.BeginForm("Index","Profile",FormMethod.Get, new { enctype="multipart/form-data" }))
The page source still has:
<body>
<form method="post" action="6111e591-b92d-4bcb-b214-ab8f664b35f9" id="form1">
I mean I can not change the tag but have no idea why :/
Try changing:-
public ActionResult Edit(string id, HttpPostedFileBase file,
FormCollection formValues)
to:-
public ActionResult Edit(string id, HttpPostedFileBase image,
FormCollection formValues)
as the name of your input is image
<input type="file" id="Image" name="Image" />
edit
To be honest something else is stopping the binding of image. Is this the whole form that you have posted?
A few things to test
You have HTTPOST decorating your method twice, although I don't believe this should make a difference.
View the source and make sure there is nothing else named name=image in the source.
Make sure you empty your cache and make sure source is correct before testing again
Try using <form action="/profile/index" method="post" enctype="multipart/form-data">
Judging by your last edit you have a problem with master pages/layout? Is this a mvc/webforms hybrid?
The solution of this problem when:
We use Master.Site,
We want to upload file in a view,
We are sure that it should work but we all the time has null,
Then:
Guys were right - I had wrong name in my view - check it!
Check source code of your view and if you have 2 < form > tags you should remove the < form > tag from Master site as then the second one is ignored!
Now it should work.
Well, in your view you named the file input 'image' but your action method accepts a parameter called 'file'. Rename one of those and it should work.