Posted In: Struts

Struts2 simple example

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	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">
	<display-name>Struts2App</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts-prepare</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter
		</filter-class>
	</filter>
	<filter>
		<filter-name>struts-execute</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts-prepare</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts-execute</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>
<?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>
	<constant name="struts.enable.DynamicMethodInvocation"
		value="false" />
	<constant name="struts.devMode" value="false" />
	<include file="login.xml" />
</struts>
<?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>
	<package name="login" namespace="/login" extends="struts-default">
		<action name="Login_*" method="{1}"
			class="com.company.strutsapp.LoginAction">
			<result name="input">/WEB-INF/jsp/login.jsp</result>
			<result type="dispatcher">
				/WEB-INF/jsp/loginSuccess.jsp
			</result>
		</action>
	</package>
</struts>
package com.company.strutsapp;

import java.io.Serializable;

public class LoginBean implements Serializable
{
  private String userId;
  private String password;

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getUserId() {
    return userId;
  }

  public void setUserId(String userId) {
    this.userId = userId;
  }
}
package com.company.strutsapp;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private final Logger logger = Logger.getLogger(LoginAction.class);

	LoginBean loginBean = new LoginBean();

	public void setLoginBean(LoginBean bean) {
		loginBean = bean;
	}

	public LoginBean getLoginBean() {
		return loginBean;
	}

	public String input() throws Exception {
		logger.info(BeanUtils.describe(loginBean));
		loginBean.setUserId("");
		loginBean.setPassword("");
		return INPUT;
	}
	
	public String execute() throws Exception {
		logger.info(BeanUtils.describe(loginBean));

		return SUCCESS;
	}
}

Tags:

by , on January 14th, 2017

  • Categories