Struts 2 -> First Simple Application
Login.java
Index.jsp
Success.jsp
Struts.xml
Web.xml
Following figure show the struts 2 application structure.
web.xml is used to configure the servlet container properties of the First Simple appliation. The filter and the filter-mapping elements are used to setup the Struts 2 FilterDispatcher. The filter is mapped to the URL pattern "/*". This means all the incoming request that targets to the Struts 2 action will be handled by FilterDispatcher class.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
We can change welcome fiel name like register.jsp or login.jsp
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="Login" class="com.Login">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
Index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
</head>
<body>
<s:form action="Login">
<s:textfield name="userName" label="User Name"></s:textfield>
<s:submit name="submit" value="Submit"></s:submit>
</s:form>
</body>
</html>
Login.java
package com;
public class Login {
private String userName;
public Login(){
}
public String execute(){
return "SUCCESS";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Success.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Information</title>
</head>
<body>
<h1><s:property value="userName" /></h1>
</body>
</html>
Type the following url in the browser "http://localhost:8080/Example1/index.jsp". The index page will be displayed.
No comments:
Post a Comment