Drop Down

Showing posts with label STRUTS. Show all posts
Showing posts with label STRUTS. Show all posts

Monday, January 28, 2019

Struts_App1

--web.xml
-------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Struts1_HelloWorld</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>1</load-on-startup>  
  </servlet>
  <servlet-mapping>
        <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>
----------------------
--Index.jsp
---------------------
<%@taglib uri="http://struts.apache.org/tags-html" prefix ="html"%>
<html:errors/>
<h1>Welcome to World</h1>
<html:form action ="hello">
Name: <html:text property="name"/>
Roll: <html:text property="roll"/>
<html:submit value ="Say Hello"/>
</html:form>
---------------------
--FormBankUp.java
---------------------
package bean;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
//This is bean class which extends ActionForm, //
public class FormBackUp extends ActionForm
{
private int roll;
private String name;

public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors ae = new ActionErrors();
if(name.equals(""))
{
ae.add("name", new ActionMessage("nameError"));
//return ae;
}
/*if("".equals(getRoll() ) )
{
ae.add("roll", new ActionMessage("rollError"));
//return ae;
}*/
return ae;
}
}

-----------------------
--HelloController.java
-----------------------
package bean;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.struts.action.*;
public class HelloController extends Action
{//This similar to controller classes ie. Servlets service()
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response)
throws Exception 
{
String name = request.getParameter("name");
int roll = Integer.parseInt(request.getParameter("roll"));
System.out.println("Name: "+name);
System.out.println("Roll: "+roll);
request.setAttribute("res","Hello......"+name);
return mapping.findForward("success");
}
}

---------------------
--Success.jsp
---------------------

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

--------------------
--struts-config.xml
--------------------
<?xml version="1.0" encoding="UTF-8"?>
<struts-config>
<form-bean>
<form-bean name="HF" type ="bean.FormBackUp"/> <!-- HF if ref. to FormBackUP -->
</form-bean>
<action-mapping>
<action path="/hello" name="HF" index="/index.jsp" type="bean.HelloController">
<forward name="success" path="/Success.jsp"/>
</action>
</action-mapping>
<message-resources parameter="bean/messages"/>


</struts-config>
--------------------------
--messages.properties
--------------------------
nameError = <font color='red'> Please Enter Name</font>
rollError = <font color='red'> Please fill Roll No.</font>

========================================================================

Java 8 Notes Pics