The first Struts2 simple example

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="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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>
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" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<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>
		</action>
	</package>
</struts>
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 User.java

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 LoginAction2.java

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";
		}else{
			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"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<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">
	</form>
</body>
</html>
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.



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