aspose file tools*
The moose likes Struts and the fly likes struts simple program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts simple program" Watch "struts simple program" New topic
Author

struts simple program

N. Ganapathy
Greenhorn

Joined: Apr 16, 2012
Posts: 24

can anyone tell me wat s the problem in this...... i am gettin an error like this


[Thumbnail for error.jpg]

Vicky Vijay
Ranch Hand

Joined: Apr 23, 2008
Posts: 125
Hi ganapathy,

Check this,

Have you specified the form and its action mapping correctly in your struts-config.xml file ?

Post your JSP file for quick response
N. Ganapathy
Greenhorn

Joined: Apr 16, 2012
Posts: 24

This s my login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!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>JSP Page</title>
</head>
<body>
<h1>LOGIN FORM</h1><table border="1" width="0">
<thead>
<tr>
<td colspan="2">
<bean:write name="LoginForm" property="error" filter="false"/>
 </td>
</tr>

<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">
<bean:write name="LoginForm" property="error" filter="false"/>
 </td>
</tr>

<tr>
<td>ENTER YOUR NAME:</td>
<td><html:text property="name" /></td>
</tr>
<tr>
<td>ENTER YOUR EMAIL ID:</td>
<td><html:text property="email"/></td>
</tr>
<tr>
<td></td>
<td><html:submit value="Login" /></td>
</tr>
</tbody>
</table>

<html:form action="/login">

<html:submit value="Login" />

</html:form>
</body>
</html>

this s my success.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<!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>LOGIN SUCCESS</title>
</head>
<body>
<h1>Congratulations!</h1>

<p>You have successfully logged in.</p>

<p>Your name is: .</p>

<p>Your email address is: .</p>
<p>Your name is: <bean:write name="LoginForm" property="name" />.</p>

<p>Your email address is: <bean:write name="LoginForm" property="email" />.</p>

</body>
</html>

this s my struts.config

<struts-config>
<form-beans>
<form-bean name="LoginForm" type="com.myapp.struts.LoginForm"/>


</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>

<action-mappings>
<action name="LoginForm" path="/login" scope="request" type="com.myapp.struts.LoginAction" validate="false">
<forward name="success" path="/WEB-INF/success.jsp"/>
<forward name="faiilure" path="/login.jsp"/>
</action>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/myapp/struts/ApplicationResource"/>


<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>

<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>

this s my action class

package com.myapp.struts;

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.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
*
* @author Administrator
*/
public class LoginAction extends org.apache.struts.action.Action {

/* forward name="success" path="" */
private final static String SUCCESS = "success";
private final static String FAILURE = "failure";


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm formBean = (LoginForm)form;
String name = formBean.getName();
String email = formBean.getEmail();

if((name == null) || // name parameter does not exist
email == null || // email parameter does not exist
name.equals("") || // name parameter is empty
email.indexOf("@") == -1) { // email lacks '@'


return mapping.findForward(FAILURE);}
else
{ return mapping.findForward(SUCCESS);
}
}
}

venkataswamy sonti
Greenhorn

Joined: Apr 14, 2012
Posts: 7
Are you write the Action Form been class LoginForm under com.myapp.struts package?
N. Ganapathy
Greenhorn

Joined: Apr 16, 2012
Posts: 24

yes i have written both LoginAction.java and LoginForm.java in com.myapp.struts ...

This is my LoginForm.java

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class LoginForm extends org.apache.struts.action.ActionForm {

private String name;
private String email;
private String error;

public String getError() {
this.error = "<span style='color:red'>Please provide valid entries for both fields</span>";
return error;
}

public void setError(String error) {
this.error = error;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

private int number;

public String getName() {
return name;
}

public void setName(String string) {
name = string;
}

public int getNumber() {
return number;
}

public void setNumber(int i) {
number = i;
}
public LoginForm() {
super();
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getName() == null || getName().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
}
return errors;
}
}
venkataswamy sonti
Greenhorn

Joined: Apr 14, 2012
Posts: 7
I think problem with your login.jsp page.. Try the following code...

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!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>JSP Page</title>
</head>
<body>
<html:form action="/login">
<h1>LOGIN FORM</h1><table border="1" width="0">
<thead>
<tr>
<td colspan="2">
<bean:write name="LoginForm" property="error" filter="false"/>
</td>
</tr>

<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">
<bean:write name="LoginForm" property="error" filter="false"/>
</td>
</tr>

<tr>
<td>ENTER YOUR NAME:</td>
<td><html:text property="name" /></td>
</tr>
<tr>
<td>ENTER YOUR EMAIL ID:</td>
<td><html:text property="email"/></td>
</tr>
<tr>
<td></td>
<td><html:submit value="Login" /></td>
</tr>
</tbody>
</table>



<html:submit value="Login" />

</html:form>
</body>
</html>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts simple program