Struts2中action简单登录

1.struts.xml文件配置action

<action name="LoginView" class="">   
     <result>/WEB-INF/login/login.jsp</result>
</action>
<action name="UserLoginAction" class="com.bict.action.Login"> 
     <result name="success">/WEB-INF/login/success.jsp</result>
     <result name="error">/WEB-INF/login/error.jsp</result>
</action>

2.登录页面login.jsp
<form action="UserLoginAction.action" method="post">
      <label>用户名:</label><input type="text" name="username"/><br/>
      <label>密码:</label><input type="password" name="password"/><br/>
      <input type="submit"/>
</form>

3.Login处理用户请求,通过ActionContext对象访问web应用的Session,返回一个Map

package com.bict.action;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Login 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;
	}
	public String execute(){
		if(getUsername().equals("linbo") && getPassword().equals("123456")){
			
			ActionContext.getContext().put("user", getUsername());//通过ActionContext对象访问web应用的Session,返回一个Map
                        return SUCCESS;
		}else{
			return ERROR;
		}
	}
}

4.在success.jsp页面中使用JSP语法输出Session中user属性

欢迎,${sessionScope.user}您已经登录!



你可能感兴趣的:(session,struts,String,user,Class,action)