Yesky首页| 产品报价| 行情| 手机 | 数码 | 笔记本 | 台式机 | DIY硬件 | 外设 | 网络 | 数字家庭 | 评测 | 软件 | e时代 | 游戏 | 图片 | 壁纸 | 群乐 | 社区 | 博客 | 下载
您现在的位置: 天极网 > 开发频道 > 实例学习Struts
全文

实例学习Struts

2003-11-25 21:10 作者: satitan 出处: yesky-matrix 责任编辑:>方舟
  创建 Controller

  LogonAction.java

package org.monotonous.struts;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.apache.commons.beanutils.PropertyUtils;

public final class LogonAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Locale locale = getLocale(request);
MessageResources messages = getResources(request);

// Validate the request parameters specified by the user
ActionErrors errors = new ActionErrors();
String username =
(String) PropertyUtils.getSimpleProperty(form, "username");
String password =
(String) PropertyUtils.getSimpleProperty(form, "password");

if ((username != "foo") || (password != "bar"))
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.password.mismatch"));

// Report any errors we have discovered back to the original form
if (!errors.isEmpty()) {
saveErrors(request, errors);
return (mapping.getInputForward());
}

// Save our logged-in user in the session
HttpSession session = request.getSession();
// Do something with session...

// Remove the obsolete form bean
if (mapping.getAttribute() != null) {
if ("request".equals(mapping.getScope()))
request.removeAttribute(mapping.getAttribute());
else
session.removeAttribute(mapping.getAttribute());
}

// Forward control to the specified success URI
return (mapping.findForward("success"));
}
}

  LogoffAction.java:

  package org.monotonous.struts;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

public final class LogoffAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();

session.removeAttribute("userattrib");
session.invalidate();

// Forward control to the specified success URI
return (mapping.findForward("success"));
}
}

  到浏览器里面欣赏一下吧,不过还不到开香槟的时候,也许你应该为这个应用考虑一些安全措施,下一次我再讲咯。

  本文由yesky与Matrix共同策划
共3页。 9 1 2 3
共3页。 9 1 2 3
网友关注
最新上市
编辑推荐
文章阅读排行
周排行
月排行
欢迎订阅天极网RSS聚合资讯:http://www.yesky.com/index.xml