Snippet Seasar
With snippet Seasar, you can free your mind
-
[SAStruts] Show Error Page When Page Does Not Exist
When a page does not exist, the error message of Apache is displayed
There is an easy method of making this change to original page in SAStruts
It is realizable by adding the following description
/webapp/WEB-INF/web.xml
The place to add is over
<jsp-config>
.<error-page> <error-code>404</error-code> <location>/WEB-INF/view/error/index.jsp</location> </error-page>
<error-code>
: writes HTTP Status<location>
: writes jsp url of original page -
[SAStruts] Create Action Method
Add annotation Execute to a method in Action Class
@Execute(validator = false) public String index() { return "index.jsp"; }
-
[SAStruts] Action, Action Method and URL
a. SAStruts default
SAStruts depend on name of Action Class to map with URL, and Action Method in Action Class should be map with sub level.
E.g. class ExampleAction include Action Method named methodName, then URL are http://domain/context/example and http://domain/context/example/methodName
b. Customize action method URL
Specified urlPattern in annotation Execute
@Execute(validator = false, urlPattern = "urlName") public String methodName() { return "index.jsp"; }
Now, URL change to http://domain/context/example/urlName
c. Pass parameters in URL for Action Method
Action Method parsing parameter from URL with urlPattern
@Execute(validator = false, urlPattern = "urlName/{year}/{month}/{day}") public String methodName() { return "index.jsp"; }
year, month, day pass to method, to get their values, parameters must be declare in Action Form
d. Warning, using urlPattern same pattern for many Action Method easy to cause confusion, sometime exception occur
@Execute(validator = false, urlPattern = "{year}/{month}/{day}") public String oneMethod() { return "index.jsp"; } @Execute(validator = false, urlPattern = "twoMethod/{month}/{day}" ) public String twoMethod() { return "index.jsp"; }
-
[SAStruts] Return View when Action Method finished
a. Return jsp
public class FooAction { @Execute(validator = false) public String foo() { return "index.jsp"; } }
SAStruts implicit return
src/main/webapp/WEB-INF/view/foo/index.jsp
b. Return Action (redirect) - REST
public class FooAction { @Execute(validator = false) public String foo() { return "/baz/plus?redirect=true"; } }
c. Return Action (redirect) with parameters
public class FooAction { @Execute(validator = false) public String foo() { return "/baz/plus?age=25&redirect=true"; } }
d. Clear Form object stored in Session Scope when Action finished normally (until JSP complete)
Flow: Edit screen => do Save (error) => Edit screen, refill value almost using Session Scope.
If Save success (no error), the temporary values become unnecessary, should be remove.
In annotation Execute, we can specify option
removeActionForm = true
, it mean framwork should be remove values in ActionForm when Action finished normally.public class FooAction { @Execute(validator = false, removeActionForm = true) public String foo() { return "/baz/plus?age=25&redirect=true"; } }
-
[SAStruts] Validate input data
a. Validate with Form
i. What's Form: See How to Use SAStruts Form
ii. Validate
Add option validator in Execute annotation is true, validate input rules may be code in Form. Further, we can specify error page when validation occur.
public class FooAction { @Execute(validator = true, input = "error.jsp") public String foo() { return "index.jsp"; } }
b. Validate data by Action Method
i. Specify validate method
Specify validate method name by option
validate
in Execute annotationpublic class FooAction { @Execute(validator = true, validate = "fooValidator", input = "error.jsp") public String foo() { return "index.jsp"; } }
Only one method accepted.
ii. Write validate method
Validate method place in Action class use this validate.
To validate input for method foo, create fooValidator.
public ActionMessages fooValidator() { ActionMessages errors = new ActionMessages(); if (!"2013".equals(fooForm.year)) { errors.add("no2013", new ActionMessage("foo.index.error.no2013")); } return errors; }
Return ActionMessages data type, if
errors
has value then go to error page.Error message got by key (e.g. no2013) from application.properties to create ActionMessage, and then added to errors.
c. Validate both validate method and Form class
At first, input validated by Form, and then validate method executed.
Default, if input validate by Form got errors, then validate method not execute.
When option stopOnValidationError is false in Execute annotation, if validate by Form got error, validate method still executed.
public class FooAction { @Execute(validator = true, validate = "fooValidator", input = "error.jsp", stopOnValidationError = false) public String foo() { return "index.jsp"; } }
d. Specify error page or action when exception occur
Use option
input
of Execute annotation to specify destination when exception occur.public class FooAction { @Execute(validator = true, validate = "fooValidator", input = "error.jsp") public String foo() { return "index.jsp"; } }
Similar with return page of Action Method
public class FooAction { @Execute(validator = true, validate = "fooValidator", input = "baz/{year}/{month}/{day}?redirect=true") public String foo() { return "index.jsp"; } }
Flow: Edit screen => do Save (error) => Edit screen
e. Put error message when exception occur into Session
Because error message default put in request scope. So when redirect, error message has lost value. If we show error message at destination after redirect, we need put error message in Session Scope.
Use option saveErrors in Execute annotation, we can specify scope to put error message.
public class FooAction { @Execute(validator = false, saveErrors = SaveType.SESSION) public String foo(){ return "/baz/plus?age=25&redirect=true"; } }
Flow: Edit screen => do Save (error) => Edit screen
-
[JdbcManager] Initialize JdbcManager
S2ContainerFactory.configure("app.dicon"); S2Container container = S2ContainerFactory.getConfigurationContainer(); JdbcManager jdbcManager = (JdbcManager) container.getComponent("jdbcManager"); // JdbcManager.class List
accounts = jdbcManager.from(Account.class).getResultList(); -
[JdbcManager] Initialize DataSourceFactory to change database
a. Create class CustomDataSourceFactoryImpl
extends
DataSourceFactoryImpl to set default database where no DataSourceName specifiedpublic class CustomDataSourceFactoryImpl extends DataSourceFactoryImpl { @Override public String getSelectableDataSourceName() { String name = (String) selectableDataSourceName.get(); if (name == null) { name = "db"; // default database } return name; } }
b. Declare component dataSource and dataSourceFactory in jdbc.dicon file
<component name = "dataSource" class="org.seasar.extension.datasource.impl.SelectableDataSourceProxy"/> <component name = "dataSourceFactory" class="mypackage.CustomDataSourceFactoryImpl"/>
c. Create DataSourceFactory to change database
S2ContainerFactory.configure("app.dicon"); S2Container container = S2ContainerFactory.getConfigurationContainer(); DataSourceFactory dataSourceFactory = (DataSourceFactory) container.getComponent("dataSourceFactory"); // DataSourceFactory.class dataSourceFactory.setSelectableDataSourceName("db1");
Comments
Post a Comment