The first Struts2 simple example

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

In this paper the use of Struts2 demonstrates a most simple login, aims to understand the Struts2.

First, look at the directory structure of the sample.

Need to introduce the JAR package as shown below

The contents of the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns=""

Note: unlike Struts1, Struts2 does not need to use the servlet tag to configure ActionServlet, replace sb. is to use the filter configuration. Of course, the core processing is different.

The Strut.xml configuration file contents

<?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

	<package name="test2" namespace="/user" extends="struts-default">
		<action name="Login" class="com.action.LoginAction2" method="aaa">
			<result name="success">/Success.jsp</result>
			<result name="error" type="redirect">/Error.jsp</result>
Note: the Strut.xml and struts1 in the struts-config.xml file, but the configuration information for this file is the configuration information for the struts-config.xml simple many, no longer need to configure the actionForm, because the Struts2 in the world has not the concept of ActionForm.

The contents of the

package com.entity;

public class User {
private String userName;
	private String password;
	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;

	public String getPassword() {
		return password;

	public void setPassword(String password) {
		this.password = password;
Note: if not to a file in struts1 corresponds to it, I feel it is in struts1 ActionForm, but with different is, where user is the POJO object, does not need any kind of inheritance.

The contents of the

package com.action;

import com.entity.User;

public class LoginAction2  {
	private User user;

	public User getUser() {
		return user;

	public void setUser(User user) {
		this.user = user;

	public String aaa() throws Exception {
//		public String execute() throws Exception {
		if ("admin".equals(user.getUserName()) && "admin".equals(user.getPassword())){
			System.out.println("Login is successful");
			return "success";
			System.out.println("Login failed");
			return "error";

Note: the Struts2 action class could not inherit from any other class, which is different from struts1, it is also what makes Struts2 promotion as a lightweight, low invasive framework. In the Struts2 core class action no longer requires a large number of parameters, which will bring great benefits to the test.

The contents of the Login.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
	<form action="user/Login.action" method="post">
		<input name="user.userName" type="text"><br>
		<input name="user.password" type="text"><br>
		<input type="submit" value="Sign in">
Note: Although the struts1 and Struts2 web.xml content of the allocation of different, but different in essence what JSP action or wood.

Implementation results show

The above is a simple Struts2 login example, about Struts2, and need to be further studied.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Hogan at December 04, 2013 - 12:53 PM