Download the NetBeans project here. File -> Download
Using JSTL 1.2 I'm trying to get my web app to remember my input and then place it into the input box for me after submitting the form but for some reason it doesn't remember it. I just have 1 .java class and .jsp file.
PersonController.java
package controller;
public class PersonController {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# page import="controller.PersonController" %>
<jsp:useBean id="personController" class="controller.PersonController" scope="session"/>
<jsp:setProperty name="personController" property="name" param="name"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>PersonController</title>
</head>
<body>
<form method="post" action="index.jsp">
<input name="name" maxlength="30" type="text" id="name" value="<c:out value="${personController.name}"/>"><br/>
<input type="submit" name="button" value="Remember my name">
</form>
</body>
</html>
Errors
HTTP Status 500 - /index.jsp (line: 4, column: 0) The value for the useBean class attribute controller.PersonController is invalid.
If it does not show error #1 then it will not populate the input field name after posting with previous input.
For your first error, I debug your code and you created one parameterized constructor. You need to add default constructor in your PersonController class. It will resolve your "The value for the useBean class attribute controller.PersonController is invalid" error.
For the value being set, You are using Controller and provided doGet method but it has not been defined as Servlet Class, It is a normal java class. You need to extend it with HttpServlet class.
Thanks.
if you only want to remember your input after you submit,there do not need PersonController.java.
just write like this:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>PersonController</title>
</head>
<body>
<form method="post" action="index.jsp">
<input name="name" maxlength="30" type="text" id="name" value="<c:out value="${param.name}"/>"><br/>
<input type="submit" name="button" value="Remember my name">
</form>
</body>
</html>
This cannot work.
The <jsp:useBean> looks for some bean with name personController in default scope, which is page, and provides it for other parts of your JSP, which is the html input tag in your case. But nobody has set this bean into the pageScope, so the useBean will create new instance of PersonController and put there. The name value is therefore null.
If you don't use any redirects, you can simply fill the input's value according to HTTP request's parameter:
<input name="name" value="<c:out value="${param.name}"/>">
If you are using HTTP redirect (you should after sending POST request), you can store the name into session.
<jsp:useBean id="personController" class="controller.PersonController" scope="session"/>
<jsp:setProperty name="personController" property="name" param="name"/>
The <jsp:setProperty> sets the name property of the previously found (or created) bean personalController to the value of the request parameter with name name.
Related
I have Simple Spring form with employee personal info and Contact Info..I have 2 bean EmpPersonalInfo and EmpContactInfo,How do i bind 2 object with "command' and show in empform.
i got this error like Invalid property 'name' of bean class [java.util.HashMap]: Bean property 'name' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
empform
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form:form action="saveemp" method="post">
<table>
<tr> <td> Name</td>
<td> <form:input path="name"/></td></tr>
<tr> <td>Id </td>
<td><form:input path="id"/> </td></tr>
<tr><td> Current Address</td>
<td><form:input path="empcontactinfo.currentAddress"/> </td> </tr>
</table>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
Empcontroll.class
#RequestMapping("/empform")
public ModelAndView showForm()
{
Map<String,Object> modelmap=new HashMap<String,Object>();
modelmap.put("personalinfo",new EmpPersonalInfo() );
modelmap.put("contactinfo", new EmpContactInfo());
return new ModelAndView("empform","command",modelmap);
}
Path attribute puts value of input field into java properties using java beans convention to be used as #ModelAttribute in your controller method.
#RequestMapping(...)
public String saveEmp(#ModelAttribute("employee") EmpPersonalInfo employee) {
// ...
}
if you just want to display the values inside these two objects in that map, you could use,
<c:out value="${command['contactinfo'].contact}"/>
This question already has an answer here:
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
(1 answer)
Closed 6 years ago.
I want to perform multiple operation Like DELETE and UPDATE to do so I Need to send Data to Controller,
where I Am doing Mistake??
Following is my JSP page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:useBean id="TimeDetailBean" class="com.logic.bean.userBean" scope="application" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Manage Results</title>
</head>
<body>
<center>
<h1>The Employee_Info Results </h1>
<table>
<tr><th>Name</th><th>Last name</th><th>Password</th></tr>
<c:forEach items="${rows}" var="row">
<tr>
<td><input type="text" name="name" value=${row.NAME}></td>
<td><input type="text" name="lastname" value=${row.LASTNAME}></td>
<td><input type="text" name="password" value=${row.PASSWORD}></td>
<td>UPDATE</td>
<td>DELETE</td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
and below is the controller where I want The value of name ,lastname and password
#RequestMapping("/delete")
public ModelAndView Delete(HttpServletRequest request, HttpServletResponse response)
{
System.out.println("Delete Controller Executed");
userBean ub= new userBean();
Dao d= new Dao();
String name=request.getParameter("name");
String lastname=request.getParameter("lastname");
System.out.println("Name catch"+name);
System.out.println("Lastname catch"+lastname);
return new ModelAndView("deleteSuccess");
}//delete ends
Thanks in advance. .
In order to send the Data from JSP to Controller
Create a form with action (/delete) and method=POST
Create a Controller with the #RequestMapping("/delete") that point to form action
Use Request.getParameter("name") in Controller.
Now on submit button in the JSP Spring Servlet Dispatcher will the mapping form in the Controller class and send the data to the controller from JSP.
Let us know if more inforamtion needed I will share the sample example
Regards,
Pavan
I have a problem with JSP Bean's scope - Request. I have a page Index.jsp with jsp bean 'message', its scope is Request and a page result.jsp. When I send request to result.jsp from Index.jsp. My bean 'message' should keep its value but it doesn't now.
I tried with scope Session and my bean worked well. I search all questions about this problem but no answer can meet my question.
Here is my code:
file Index.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
String name = request.getParameter("name") == null ? "" :
request.getParameter("name");
int age = ( request.getParameter("age") == null ||
request.getParameter("age") == "") ? 0 :
Integer.parseInt(request.getParameter("age"));
%>
<h1>Nice to meet you</h1>
<form method="post" action="View/result.jsp">
<jsp:useBean id="message" class="com.java.Message" scope="request"/>
<jsp:setProperty name="message" property="message" value="Hello world!"/>
<label>Name: </label> <br>
<input type="text" name="name" placeholder = "Phan Dinh The"/> <br>
<label>Age: </label> <br>
<input type="number" name="age" placeholder = "25"/> <br>
<input type="checkbox" name="title"/> Senior <br>
<input type="radio" name="language" value="c#"/> C# <br>
<input type="radio" name="language" value="java"/> Java <br>
<br><br>
<jsp:include page="View/date.jsp" flush="true"/>
<input type="submit" value="submit"/>
<br><br>
</form>
<br><br>
</body>
</html>
file result.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.java.Message"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="message" class="com.java.Message" scope="request"/>
<jsp:getProperty name="message" property="message"/>
</body>
</html>
my class Message
package com.java;
public class Message {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String content) {
this.message = content;
}
}
I use Tomcat 8.0.23, Jsp version 2.3, Servlet API 3.1
When you use
<jsp:setProperty name="message" property="message" value="Hello world!"/>
in the index.jsp file, that property is scoped to the request of the index.jsp page. once the index jsp page returns to the client, that request is done. When you submit the form, a new request is created, and that is used for the result page generation. Thus when you are in the result.jsp code, there is no request scoped parameter named 'message'.
You could always put the message in an
<input type="hidden" name="message">Hello World</input>
field of the form, and retrieve it in the results.jsp that way.
Hope someone just could give a hint where to look for the source of the problem.
Class Login Action
enter code here
#Namespace("/")
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
#Override
#Action(value = "/welcome", results = { #Result(name = SUCCESS, location = "/WEB-INF/content/welcome.jsp") })
When I exectute my index.jsp to execute welcome.jsp didn't work.
index.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<h3>Welcome to Struts 2</h3>
<s:form action="home">
<s:textfield name="username" label="User Name"></s:textfield>
<s:textfield name="password" label="Password" type="password"> </s:textfield>
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
welcome.jsp
enter code here
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=IUTF-8">
<title>Welcome To Struts 2</title>
</head>
<body>
<h3>
Welcome
<s:property value="username"></s:property>
!
</h3>
</body>
Result
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts].
In Struts2 you map a form to the action config using s:form tag's action and namespace attributes.
Normally, the action attribute accepts the action name, but it can contain the URL. The form tag is rendered by the template to HTML form where the action attribute have to be URL.
So Struts is getting the action name and generate URL. You can see the source code in the browser. When request is made Struts2 uses the request URI to find the action mapping as explained here.
Usually the 404 error results in missing action configuration on the server that corresponds to the requested URI. When you submitted a form the request is made and resource is requested but the action config is not found, hence Struts2 returns 404 error.
You don't need to use annotations if you are utilizing a convention or rest plugins but the annotation always allows manually override the defaults.
By default the results path is /WEB-INF/contents/ and the action name is without slashes, so you can use annotation:
#Action(value = "welcome", results = #Result(location = "welcome.jsp") )
You can find the references to online resources from this answer.
The one from tutorials read this tutorial.
The solution.
In my index.jsp I wrote "home" in the tag action, form action="home" The correct are the same name at the Action.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<h3>Welcome to Struts 2</h3>
<s:form action="home"> **The correct is "welcome"**
<s:textfield name="username" label="User Name"></s:textfield>
<s:textfield name="password" label="Password" type="password"> </s:textfield>
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
And in my LoginAction I used value ="/welcome".
package web.actions;
#Action(value = "/welcome", results = { #Result(name = SUCCESS, location = "/WEB-INF/content/welcome.jsp") })
public String execute() throws Exception {
return SUCCESS;
}
The messagem is very clear.
There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts].
But I just saw the mistake doing a lot of examples.
This is my HTML form :
<form action="supplierportal_home.jsp">
<select id="contract" name="contract">
<option selected="selected">Please Select</option>
<option value="open" >Open</option>
<option value="limited" >Limited</option>
</select>
<input type="text" name="cpv_code" placeholder="<%= cpvOrTenderNo %>">
<button type="submit">FIND <%= contractOrTender %></button>
</form>
And i am getting in JSP
String contract=request.getParameter("contract");
System.out.println("%%%"+contract);
String cpv_code=request.getParameter("cpv_code");
System.out.println("%%%"+cpv_code);
Here is the problem.Seems very nasty.
When I enter both the values then only parameters getting in jsp If I select only the contract from options then null is coming if i give cpv-code then the parameter is coming ...can any one please help to get out this ...
Why my select option values are depending on the other form element cpv-code value,please show some cause for this.
Thanks.
<button type="submit">FIND <%= contractOrTender %></button>
In this example whenever you will enter text in textbox then only contractOrTender
this value can be objtain from textbox.
But as you not entering any value in textbox and trying to access value from dropdown it will come 'null' only. as boz at this time <%= contractOrTender %> this value is null.
which an error in jsp page. thats why you not getting dropdown value even if you dont enter any value in textbox.
Solution
Try to set some default value to textbox. or
change your HTML code.
I have just tested the code and it seems to work fine. No value is depending on the other. Only suggestion is add method="POST" if you don't want the data to be sent over URL.
index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
<form action="upload.jsp" method="POST">
<select id="contract" name="contract">
<option selected="selected">Please Select</option>
<option value="open" >Open</option>
<option value="limited" >Limited</option>
</select>
<input type="text" name="cpv_code" />
<button type="submit">FIND</button>
</form>
</body>
</html>
upload.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload JSP</title>
</head>
<body>
<%
String contract=request.getParameter("contract");
System.out.println("%%%"+contract);
String cpv_code=request.getParameter("cpv_code");
System.out.println("%%%"+cpv_code);
%>
</body>
</html>