service層編寫商業邏輯總會有很多的返回信息,那么如何合理的和struts/webwork結合起來呢,我參考了spring論壇上的一個做法如下:
1
/** *//**
2
*
3
*/
4
package it.linksystem.csai.web.util;
5
6
import java.util.Iterator;
7
8
import it.linksystem.csai.client.delegate.UserBusinessDelegate;
9
import it.linksystem.csai.common.Error;
10
import it.linksystem.csai.common.Warning;
11
import it.linksystem.csai.common.dto.UserDTO;
12
13
import javax.servlet.http.HttpServletRequest;
14
15
import org.apache.struts.action.ActionMessage;
16
import org.apache.struts.action.ActionMessages;
17
18
import net.sf.acegisecurity.Authentication;
19
import net.sf.acegisecurity.AuthenticationException;
20
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
21
import net.sf.acegisecurity.ui.AbstractProcessingFilter;
22
23
/** *//**
24
* @author Srepfler Srgjan
25
*
26
*/
27
public class LoginProcessingFilter extends AbstractProcessingFilter
{
28
29
/**//* (non-Javadoc)
30
* @see net.sf.acegisecurity.ui.AbstractProcessingFilter#getDefaultFilterProcessesUrl()
31
*/
32
public String getDefaultFilterProcessesUrl()
{
33
return "/LoginSubmit.do";
34
}
35
36
/**//* (non-Javadoc)
37
* @see net.sf.acegisecurity.ui.AbstractProcessingFilter#attemptAuthentication(javax.servlet.http.HttpServletRequest)
38
*/
39
public Authentication attemptAuthentication(HttpServletRequest request)
40
throws AuthenticationException
{
41
ActionMessages errors = new ActionMessages();
42
ActionMessages warnings = new ActionMessages();
43
44
UserBusinessDelegate ubd = new UserBusinessDelegate();
45
String username = request.getParameter("j_username");
46
String password = request.getParameter("j_password");
47
if(username == null)
{
48
username ="";
49
}
50
if(password == null)
{
51
password="";
52
}
53
UserDTO userDTO = new UserDTO();
54
userDTO.setUsername(username);
55
userDTO.setPassword(password);
56
UserDTO resultDTO = ubd.login(userDTO.getUsername(),userDTO.getPassword());
57
58
if(resultDTO.isError())
{
59
for (Iterator theiterator = resultDTO.errorsIterator(); theiterator.hasNext();)
{
60
Error theerror = (Error) theiterator.next();
61
errors.add(theerror.getCode(),new ActionMessage(theerror.getCode()));
62
}
63
} else
{
64
if(resultDTO.isWarning())
{
65
for (Iterator theiterator = resultDTO.warningsIterator(); theiterator.hasNext();)
{
66
Warning thewarning = (Warning) theiterator.next();
67
warnings.add(thewarning.getCode(),new ActionMessage(thewarning.getCode()));
68
}
69
}
70
request.getSession().setAttribute(Constants.USER_KEY,resultDTO);
71
logger.info("Login dell utente: "+resultDTO.getUsername());
72
}
73
74
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,password);
75
authRequest.setDetails(request.getRemoteAddr());
76
return this.getAuthenticationManager().authenticate(authRequest);
77
}
78
79
}
80
DTO撤掉,然后并到service層的BaseService中,做個接口用來記錄error message,然后Baseservice的實現中提供Errors message add和Iterator,以及iserror,iswarning,然后其他service調用其方法,至于view層調用則跟上述類似。


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23


24

25

26

27



28

29


30

31

32



33

34

35

36


37

38

39

40



41

42

43

44

45

46

47



48

49

50



51

52

53

54

55

56

57

58



59



60

61

62

63



64



65



66

67

68

69

70

71

72

73

74

75

76

77

78

79

80
