Struts 2
Login example with only 1 action (Part-1)
Struts2 core components are:
1. Action Classes
2. Interceptors
3. Result Pages, JSP of FreeMarker
templates
4. ValueStack, OGNL and Tag
Libraries
Web.xml
Request ----> web.xml (Filter
Dispatcher) ----> struts.xml ----> ActionProxy(Action class)
Filters acts as Interceptor (request/response
parameters) and required to send request from web.xml to struts.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Index.jsp
<%@ taglib prefix="s"
uri="/struts-tags" %>
<html>
<body>
<s:form action="verify">
<s:textfield name="uname"
label="Enter Username" /><br>
<s:password name="password"
label="Enter Password" /><br>
<s:submit value="Click"
align="center" />
</s:form>
</body>
<html>
<html>
Struts.xml
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD
Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include
file="struts-default.xml"/>
<package
name="a" namespace="/tutorials"
extends="struts-default">
<action
name="getTutorials" class="com.javaetutorials.action.TutorialAction">
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
</action>
</package>
</struts>
TutorialAction.java (Action Class) --->struts.xml
---> success.jsp/error.jsp
public class TutorialAction
{
public String
execute() {
System.out.println("Entry in
execute method");
return "success";
}
}
Success.jsp
<%@ taglib prefix="s"
uri="/struts-tags" %>
SUCCESS PAGE
error.jsp
<%@ taglib prefix="s"
uri="/struts-tags" %>
Login
failed...!!
tutorials acts as namespace
SUCCESS PAGE
No comments:
Post a Comment