wood burning stoves 2.0*
The moose likes Struts and the fly likes how the value in the form pass into Action class in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how the value in the form pass into Action class in Struts" Watch "how the value in the form pass into Action class in Struts" New topic
Author

how the value in the form pass into Action class in Struts

sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Hi, I'm new to Struts.
I created a simple form, it use the text input to send the form data to the server. But in the Action class when I try to get the value from the ActionForm, it has null value(so it throws null pointer exception)?
Where should I check the code?

Thanks!
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Struts is a great tool. You first should check the code of the Action class. Copy and paste in a post here so we can see, if you don't mind.

The next place you might look is in the Java Server Page that has the data input form.
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Thanks, James. Here's the code.

struts-config.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans>
<form-bean name="loginForm"
type="cn.itcast.LoginForm"/>
</form-beans>

<action-mappings>

<action path="/login"
type="cn.itcast.LoginAction"
name="loginForm">
<forward name="loginSuccess" path="/LoginSuccess.jsp"/>
<forward name="loginFailure" path="/LoginFailure.jsp"/>
</action>

</action-mappings>
</struts-config>


Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>

Login.jsp:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSF 'Login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="<%=path%>/login.do" method="post">
username:<input type="text" name="username"><br>
password:<input type="text" name="password"><br>
<input type="submit" value="login">
</form>
</body>
</html>

LoginAction.java:
package cn.itcast;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

LoginForm loginForm = (LoginForm)form;
if (loginForm.getUsername().equalsIgnoreCase("itcast")){

return mapping.findForward("loginSuccess");
}else{
return mapping.findForward("loginFailure");
}

}

}

Exception at if (loginForm.getUsername().equalsIgnoreCase("itcast"))
Cendy Van
Greenhorn

Joined: Nov 06, 2007
Posts: 6
Replace tags form and html by tags html:form and html:text like this :
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

please can you post your LoginForm
Raza Khan
Greenhorn

Joined: Nov 19, 2008
Posts: 15
Hi Sarah.. can you give me your email id... i have all in a tutorial that you require.

Thanks,
Khan
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Raza Khan:
Hi Sarah.. can you give me your email id... i have all in a tutorial that you require.


Cheers. thats the sprit
Raza Khan
Greenhorn

Joined: Nov 19, 2008
Posts: 15
ya sure... i can mail the tutorial to her...

what is your point Mr. VenkataRaman...
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
sarah, in the Login.jsp you should have declarations for the Struts HTML JSP tag library and should be using the Struts form tags.
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Thanks a lot, everyone! My email july4u2001@yahoo.com

LoginForm.java:

package cn.itcast;

import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm {
private String username = null;
private String password = null;
public String getUsername() {
return username;
}
public void setUsernmae(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Any idea?

Happy Holidays!
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2053
Thanks for posting your form class.

Which field is reported null?

If it is the username, the reason is 'mistype':



It should be setUsername, not setUsernmae.
[ December 27, 2008: Message edited by: Jesus Angeles ]
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
It's a typo in the form.
Thanks a lot, Jesus and everybody!
Raza Khan
Greenhorn

Joined: Nov 19, 2008
Posts: 15
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how the value in the form pass into Action class in Struts