Action processing class:
package com.crazyit.app.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.convention.annotation.*; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; @Namespace("/") @Results({ @Result(name="success",location="/WEB-INF/content/welcome.jsp") }) public class loginAction extends ActionSupport{ 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; } @Override public String execute() throws Exception { if("lidian".equals(username) && "123".equals(password)) { return SUCCESS; }else { return ERROR; } } @Action( "login" ) public String login() throws Exception{ if ("lidian".equals(username) && "123" .equals(password)) { /* Set application scope properties */ ActionContext.getContext() .getApplication().put( "counter", 1 ); /* Set the properties in the session scope */ ActionContext.getContext().getSession().put( "username" , username); /* Set the properties in the request scope * / ActionContext.getContext().put( "tip", "You are logged in" ); return SUCCESS; }else { return ERROR; } } }
jsp access data:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!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=UTF-8"> <title>welcome</title> </head> <body> <h1>欢迎,${sessionScope.username }</h1> <h2>当前访问量:${applicationScope.counter }</h2> <h3>提示消息:${requestScope.tip }</h3> </body> </html>