1 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
public class AbstractSimpleFormController extends SimpleFormController implements BaseController {
private transient ServiceLocator serviceLocator;
private WebApplicationContext context;
private Integer accessLevel; // access level is unmutable
private boolean redirect;
private String urlRedirect;
// this map is used to retrieve a view via a value (see struts "findForwared")
private Map<String, String> conditionalViews;
public AbstractSimpleFormController() {
// by default the user is authorized the view the page
this(0);
}
public AbstractSimpleFormController (Integer accessLevel) {
super();
if (accessLevel == null) {
throw new IllegalArgumentException("access level can not be null");
}
this.accessLevel = accessLevel;
// Spring imposes to the developpers to set the view name
// but in some case the developper won't do it because he/she preffers to use the redirect mechanisme.
// So by setting an empty view name, we make the developer's life more easy.
setSuccessView("");
// the developper must precise He/She would like to use the redirect mechanism.
setRedirect(false);
}
public void setConditionalViews(Map<String, String> conditionalViews) {
this.conditionalViews = conditionalViews;
}
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
if (!redirect) {
return super.onSubmit(request, response, command, errors);
} else{
return new ModelAndView(new RedirectView(urlRedirect));
}
}
protected ModelAndView onSubmit(HttpServletRequest request, String value) throws Exception {
if (!redirect) {
return new ModelAndView(conditionalViews.get(value));
} else{
return new ModelAndView(new RedirectView(conditionalViews.get(value)));
}
}
public boolean isRedirect() {
return redirect;
}
public void setRedirect(boolean redirect) {
this.redirect = redirect;
}
public String getUrlRedirect() {
return urlRedirect;
}
public void setUrlRedirect(String urlRedirect) {
this.urlRedirect = urlRedirect;
}
@Override
protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
Map reference = super.referenceData(request, command, errors);
if (reference == null) {
reference = new HashMap();
}
return reference;
}
@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
Map reference = super.referenceData(request);
if (reference == null) {
reference = new HashMap();
}
return reference;
}
public Object getSpringBean(String beanId) {
if (context == null) {
context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
}
return context.getBean(beanId);
}
public ServiceLocator getServiceLocator() {
if (serviceLocator == null) {
serviceLocator = (ServiceLocator) getSpringBean("serviceLocator");
}
return serviceLocator;
}
public Integer getAccessLevel() {
return accessLevel;
}
} |
Partager