How use Bean Tags and Message Resources in Struts

Here I am explaining how use Bean Tags and Message Resources in Struts .

SampleForm.java

package com.MessageResources;

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;
}

}

SampleAction.java

package com.MessageResources;

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");

}

}

ApplicationResources.properties

hello.name=Enter your name
hello.submit=Enter
hello.welcome=Welcome To

beansample.jsp

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

<body>
<br><br>
<center><h2><u>Sample Struts Form</h2></u>
<font color="red"> These Text messages from properties see code.<br><br></font>
<html:form action="/sample">

<bean:message key="hello.name" /><html:text property="name" /><br><br>
<html:submit><bean:message key="hello.submit" /></html:submit>

</html:form>

</body>
</html:html>

beansuccess.jsp

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

<html>
<body><br><br>
<h2><center>
<bean:message key="hello.welcome" /><%=(String)session.getAttribute("name")%>
</body>
</html>

web.xml

<web-app>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<welcome-file-list>
<welcome-file>beansample.jsp</welcome-file>
</welcome-file-list>
</web-app>

struts-config.xml

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

<action-mappings>
<action path="/sample"
name="samplebean"
type="com.MessageResources.SampleAction"
input="beansample.jsp"
scope="request">
<forward name="success" path="/beansuccess.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>

No comments: