KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > struts > tests > DynamicAction


1 // StrutsTestCase - a JUnit extension for testing Struts actions
2
// within the context of the ActionServlet.
3
// Copyright (C) 2002 Deryl Seale
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the Apache Software License as
7
// published by the Apache Software Foundation; either version 1.1
8
// of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// Apache Software Foundation Licens for more details.
14
//
15
// You may view the full text here: http://www.apache.org/LICENSE.txt
16

17 package servletunit.struts.tests;
18
19 import org.apache.struts.action.*;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import javax.servlet.http.HttpSession JavaDoc;
24
25 public class DynamicAction extends Action {
26
27     public ActionForward execute(ActionMapping mapping,
28                                  ActionForm form,
29                                  HttpServletRequest JavaDoc request,
30                                  HttpServletResponse JavaDoc response) {
31     
32     DynaActionForm dynaForm = (DynaActionForm) form;
33         String JavaDoc username = (String JavaDoc) dynaForm.get("username");
34         String JavaDoc password = (String JavaDoc) dynaForm.get("password");
35
36         ActionErrors errors = new ActionErrors();
37
38         if ((!username.equals("deryl")) || (!password.equals("radar")))
39             errors.add("password",new ActionMessage("error.password.mismatch"));
40
41         if (!errors.isEmpty()) {
42             saveErrors(request,errors);
43             return mapping.findForward("login");
44         }
45
46         HttpSession JavaDoc session = request.getSession();
47         session.setAttribute("authentication", username);
48
49         // Remove the obsolete form bean
50
if (mapping.getAttribute() != null) {
51             if ("request".equals(mapping.getScope()))
52                 request.removeAttribute(mapping.getAttribute());
53             else
54                 session.removeAttribute(mapping.getAttribute());
55         }
56
57         // Forward control to the specified success URI
58
return mapping.findForward("success");
59
60     }
61
62 }
63
64
Popular Tags