struts2 action编程

struts2 action中属性与表单中提交的字段的关系如下,在使用时可以自动获取对应的值,无需手动获取。


package com.yht.web;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yht.model.User;
import com.yht.service.UserService;
import com.yht.util.AppException;

public class Login extends ActionSupport {

	private String name;
	private String password;
	private String massage;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getMassage() {
		return massage;
	}
	public void setMassage(String massage) {
		this.massage = massage;
	}
	
	@Override
	public String execute() {
		String result="";
		try {
			int id = new UserService().login(name, password);
			User u = new User();
			u.setName(name);
			u.setId(id);
			u.setPassword(password);
			if(id!=0){
				massage = "登录成功";
				ActionContext context = ActionContext.getContext();
				Map<String,Object> session = context.getSession();
				session.put("userId", id);
				session.put("username", name);
				int role = new UserService().getUserRole(id);
				if(role==0)//普通编辑
				{
					result = "toEditor";
				}else if(role==1)//管理员
				{
					result = "toAdmin";
				}
				
				
			}else{
				result = "toLogin";
				massage = "登录失败";
			}
		} catch (AppException e) {
			result = "error";
			massage = "系统异常"+ e.getMessage();
		}
		return result;
	}
	
}

在struts.xml中配置这个action

 <package name="news" extends="struts-default">
<!-- 接受登录信息,分发登录结果 -->
   		<action name="login" class="com.yht.web.Login">
   			<result name="toEditor" type="redirectAction">toEditor</result>
   			<result name="toAdmin" type="redirectAction">toAdmin</result>
   			<result name="toLogin" type="dispatcher">/login.jsp</result>
   		</action>
   </package>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值