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层调用则跟上述类似。
posted on 2005-09-28 22:30
老妖 阅读(824)
评论(0) 编辑 收藏