Simple Example in Struts

Here I am giving basic example on struts

sampleForm

package com.simplestruts;

import org.apache.struts.action.ActionForm;

/**
*
* @author Prasanthi
*
*/

public class SampleForm extends ActionForm{

String name=null;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

sample Action

package com.simplestruts;

import org.apache.struts.action.*;
import javax.servlet.http.*;

/**
*
* @author Prasanthi
*
*/

public class SampleAction extends Action{
public ActionForward execute(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)throws Exception
{
SampleForm sample=(SampleForm)form;

String n1=sample.getName();

HttpSession ses=req.getSession(true);
ses.setAttribute("name",n1);

return map.findForward("success");

}

}

struts-config.xml

<struts-config>
<form-beans>
<form-bean name="samplebean" type="com.simplestruts.SampleForm" />
</form-beans>

<action-mappings>
<action path="/sample"
name="samplebean"
type="com.simplestruts.SampleAction"
input="sample.jsp"
scope="request">
<forward name="success" path="/success.jsp" />
</action>
</action-mappings>
</struts-config>

sample.jsp

<%@ taglib uri="/tags/struts-html" prefix="html" %>

<html:html>

<body>
<br><br>
<center><h2><u>Sample Struts Form</h2></u><br>
<html:form action="/sample">

Enter Your Name:<html:text property="name" /><br><br>
<html:submit value="submit" />

</html:form>

</body>
</html:html>

success.jsp

<html>
<body><br><br>
<h2><center>
Welcome To <%=(String)session.getAttribute("name")%>


</body>
</html>

No comments: