Struts Java Framework2

web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>HelloWorld</display-name>
    <welcome-file-list>
<welcome-file>
index.jsp</welcome-file>
</welcome-file-list>


<servlet>
<servlet-name>actionservlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>




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

</servlet-mapping>


 
</web-app>




index.jsp


BuildPath -> Configuration BuildPath-> lib-> MyJar

<%@taglib uri="http://struts.apache.org/tags-html"  prefix="html"%>

<h1>Hello Form</h1>

<html:form action="hello">
NAME:<html:text property="name"/>
<html:submit value="SayHello"></html:submit>

</html:form>


We Create Package for Form Back Up Class "HelloFormBackUp.Java"

package beans;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts2.components.ActionMessage;

public class HelloFormBackup extends ActionForm {

private String name;

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

}

public String getName(){
return name;
}

@Override
    public ActionErrors validate(ActionMapping mapping,

HttpServletRequest request){
ActionErrors ae = new ActionErrors();

if (name.equals(""))
ae.add("name", new ActionMessage("msg"));
return ae;
}
}



messages.properties

msg=<font color='red'>name is required</font>



We made one class "HelloController.java" 



package beans;

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 HelloController extends Action
{

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

String name=request.getParameter("name");
request.setAttribute("res","hello...."+name);

return mapping.findForward("success");



}
}

File Success.jsp

<%=request.getAttribute("res")%>


File struts-config.xml



<struts-config>
<form-beans>
<form-bean name="HF" type="beans.HelloFormBackup"/>

</form-beans>
<action-mappings>
<action path="/hello" name="HF" input="/index.jsp">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>

<message-resources parameter="beans/Messages"/>
</struts-config>