Jsp:java.lang.NumberFormatException: null - java

Home.jsp Post aid to Edit.jsp
<input type="button" value="修改" onclick="location.href='Edit.jsp?aid=<%=art.getAsId()%>'"/>
Edit.jsp Get aid.
String aid = request.getParameter("aid");
out.print(aid);
if (aid == null)
response.sendRedirect("Home.jsp");
else {
IAsset ad=new AssetDao();
AssetName art=ad.getInfo(Integer.parseInt(aid));
}
EditControl.jsp
but in this page, it always tell me aid =null.
request.setCharacterEncoding("utf-8");
AssetName art= new AssetName();
art.setAsId(Integer.parseInt(request.getParameter("aid")));
art.setUID(request.getParameter("UID"));
art.setAsnu(request.getParameter("asnu"));
art.setAsnm(request.getParameter("asnm"));
art.setAste(request.getParameter("aste"));
art.setSpec(request.getParameter("spec"));
And Report
java.lang.NumberFormatException: null

<input type="button" value="修改" onclick="location.href='Edit.jsp?aid=<%=art.getAsId()%>'"/>
In this line you missed to give name of the input field
so you need to change it like this
<input type="button" name="aid" value="修改" onclick="location.href='Edit.jsp?aid=<%=art.getAsId()%>'"/>
and change parsing code like this
AssetName art=ad.getInfo(Integer.valueOf(aid));

Related

Is it possible to have an input path for variables in Thymeleaf

I know the title might be misleading but here my question: In Thymeleaf we set request params with the input (in HTML). Is it possible to have an input field that sets the path variable. For example I have an method like this:
#PostMapping("/house/{id}/rent")
public String rentHouse(#RequestParam Date startDate, #PathVariable("id") long id, Model model) {
House h = new House();
h.setId(id);
r.setStartDate(startDate);
Rents rents = rentsService.createNewRent(h, id);
model.addAttribute("rent", rents);
return "House";
}
And in House.html I want something like this:
<form th:action="#{/house/${id}/rent/}" method="post">
<label for="startDate">start Date:</label><br>
<input type="datetime-local" id="startDate" th:name="startDate" placeholder="startDate"><br>
<label for="id">house id:</label><br>
<input type="number" id="id" th:name="id" placeholder="id"><br>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
So that when I input something then the result url should be looking like this (I know start Date has false format):
localhost:8080/House/12/rents?startDate=02.21.22
And is it also possible to pass request body in Thymeleaf, I searched for similar questions but they all solved it by manually putting the path variable in the url.
Thanks in advance

freemarker how print variable if it not null and not print if it is null?

I have this model
ModelAndView modelAndView = new ModelAndView("login");
String msisdn = request.getParameter("msisdn");
modelAndView.addObject("msisdn", msisdn); //may be NULL
return modelAndView;
and page where
<#if msisdn??>
<input type="text" class="form-control" placeholder="phone" value="${msisdn}">
<#else>
<input type="text" class="form-control" placeholder="phone">
</#if>
If msisdn == null I want show placeholder="phone" but if msisdn not null I want show it.
It is work but I think it is bad practic. I not want copy all string and dublicate it in code. Can I write like this?
<input type="text" class="form-control" placeholder="phone" value="<#if msisdn != null>${msisdn}</#if>">
or
or something else in freemarker?
use ! to do this:
<input type="text" class="form-control" placeholder="phone" value="${msisdn!}">

Set a checkbox in Javascript dynamically

Can someone please tell me how to set a checkbox dynamically in Javascript?
function displayResults(html){
var v = html.split(",");
document.getElementById('fsystemName').value = v[0];
if (v[1] == "true"){
document.getElementById('fUpdateActiveFlag').checked = true;
}
}
So I am passing some values from my controller separated by commas. For the checkbox, when the value is true I want it to tick the box.
EDIT:
When a value is changed from a dropdown box it calls this displayResults method as its return statement:
$('#uINewsSystemList').change(function() {
$.get("/live-application/SystemById", {systemid: $("#systemList").val()}, displayResults, "html");
I want it to update some other values such as textboxes and checkboxes. I can get the fsystemName to populate with the appropriate value but the fUpdateActiveFlag always stays unchecked.
In your question, you posted a javascript example, not JSP.
In JSP you can use a for loop to write the checkbox like this:
<% for (Element element : elementList) { %>
<input type="checkbox" name="<%=element.getName() %>" value="<%=element.getValue() %>" <%=element.getChecked() ? "checked" : "" %> />
<% } %>
Will result in:
<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>

Nullpointer exception in doPost

I have de following Form:
<form action="/AppStore/publish" method="post" accept-charset="ISO-8859-1">
<fieldset>
<legend>Do you have an Account already?</legend>
<input type="radio" name="registred" value="yes"> Yes
<input type="radio" name="registred" value="no"> No
</fieldset>
<fieldset>
<legend>About your App</legend>
<table>
<tr>
<td><label for="AppDesc">Describe it:</label></td>
<td><input type="text" name="AppDesc" /></td>
</tr>
<tr>
<td><label for="AppName">Name:</label></td>
<td><input type="text" name="AppName" /></td>
</tr>
</table>
</fieldset>
<input type="submit" value="Submit" />
</form>
I pass this data to a Java Servlet, but every time I get a Nullpointer Exception at getParameter("AppDesc"), instead getParameter("AppName") works fine, what do I wrong?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
RequestDispatcher dispetcher = context.getRequestDispatcher("/publishForm.jsp");
List<String> errorMessages = new ArrayList<String>();
//Validating form input...
if(request.getParameter("AppName").toString().isEmpty())
{
errorMessages.add("Please type a valid Name for your App.");
}
if(request.getParameter("AppDesc").toString().isEmpty())
{
errorMessages.add("The Description of your App should contain at least 160 Characters.");
}
You're calling request.getParameter("...").toString().
request.getParameter() already returns a string reference, so you don't actually need to call toString() to get the value as a string, but it will return a null reference if the parameter is missing - in which case calling toString() will throw an exception. You need to check whether the value is null or empty. For example:
String description = request.getParameter("AppDesc");
if (description == null || description.isEmpty())
...
Of course, there are libraries around to check for "null or empty" - for example, in Guava you could use:
if (Strings.isNullOrEmpty(description))
If request.getParameter("AppDesc") is null, then
request.getParameter("AppDesc").toString().isEmpty() will throw a NullPointerException.
Why not change the condition to:
if(request.getParameter("AppDesc") == null ||
request.getParameter("AppDesc").toString().isEmpty()))
{
<td><input type="text" name="AppDescr" /></td>
You've named the actual field AppDescr (notice the trailing "r"), but you're calling getParameter for AppDesc (sans "r").
EDIT: Or not... you edited your post and fixed it. Was this not the problem?
It must be the case that request.getParameter("AppDesc") returns a null value, causing the chained toString() to generate a NullPointerException.
This parameter was never set; the name specified in the html was "AppDesr" (note the trailing 'r').
Your question title says doGet, your code says doPost. The difference between those two might explain your problem. :-)

html to jsp email form post - null parameters

I have a simple form
form action="email.jsp" method="post"
<label for="firstname">Your Name: </label>
input type="text" id="name"<br/>
<label for="email">Your Email: </label>
input type="text" id="address"<br/>
<label for="message">Message: </label>
textarea size="30" rows="4" class="expand" id="comments"</textarea<br/>
input type="submit" value="Send" input type="reset"
/form
and am posting to a email.jsp page running in tomcat 5.5, working for another website i use that is in flash
this email.jsp is coming up with null values every time i post data to it - code below
can anyone see what i'm doing wrong?
<%# page import="sun.net.smtp.SmtpClient, java.io.*, javax.servlet.http.HttpServletResponse, javax.servlet.jsp.PageContext" %>
<%
String name = request.getParameter("name");
out.print(name);
String address = request.getParameter("address");
String comments = request.getParameter("comments");
String from="test#there.com.au";
String to="test#where.com";
try{
SmtpClient client = new SmtpClient("localhost");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("From: " + from);
message.println("To: " + to);
message.println();
message.println("Enquiry :-) from " + name + " at " + address);
message.println();
message.println("Details: "+ comments);
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
out.print("Email Sent Correctly");
%>
Your HTML is a bit garbled, but it looks like you're not specifying the "name" attribute for your inputs. The "id" attribute is good for referencing your field from a <label> or for accessing your inputs from Javascript, but the browser will not use it in the POST request.
The solution is simple, add the "name" attribute to your input elements, like this:
<input type="text" id="name" name="name" />

Categories

Resources