Basic exercise:
Using Strut 2 to develop login application.
If login fail
If login successful
1. Create Java web application base on Strut2 framework as figure below.
2. Create some file as structure
3. Open struts.xml and modify as
4. Open appResources: create some keys
5. Open Login class and add new code as
6. Generate getter/setter for two field above
7. Next, design login page
8. Next, index.jsp page
9. Next, error.jsp page
10. Now, deploy and run login.jsp page
Strut2-Validation
Using Strut validate input data
If input data invalid
If input data is valid
1. Design customer.jsp as
2. Add some new key to appResources
3. Assign Customer action in struts.xml
4. Add new class with name “Customer” into codes package
5. Generate setter and getter for 4 fields
6. Add new file “Customer-validation.xml” into package
Now, re-deploy and run
Using annotations
All action class must be inside actions package
Strut Controller now change to org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
1 <filter>
2 <filter-name>struts2</filter-name>
3 <filter-class>
4 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
5 </filter-class>
6 <init-param>
7 <param-name>actionPackages</param-name>
8 <param-value>codes</param-value>
9 </init-param>
10 </filter>
11 <filter-mapping>
12 <filter-name>struts2</filter-name>
13 <url-pattern>/*</url-pattern>
14 </filter-mapping>
If your Action extend from ActionSupport, then your Action class name has no rule required.
Ex: Login action beloved
Otherwise, action class name must end with Action string
Ex: account action
1 @Namespace("/")
2 @ResultPath(value = "/")
3 @Results({
4 @Result(name = "success", location = "index.jsp"),
5 @Result(name = "error", location = "error.jsp")}
6 )
7 public class accountAction {
8 @Action(value="account")
9 public String execute() throws Exception {
10 if ("nvmit".equals(getName())) {
11 return SUCCESS;
12 } else {
13 return ERROR;
14 }
15 }
16 private String name ="";
17 public String getName() {
18 return name;
19 }
20 public void setName(String name) {
21 this.name = name;
22 }
23 }
@Namespace Annotation in Struts 2
@Namespace is used at class level or package level. This helps to change the namespace for action class. While accessing or calling action class, it hides package structure. When namespace is applied at package level, all the action of that package gets that namespace as default.
1 @Results({
2 @Result(name = "SUCCESS", location = "/user/index.jsp"),
3 @Result(name = "ERROR", location = "/user/error.jsp")
4 })
5 @Namespaces(
6 @Namespace("/user")
7 )
8 public class Login extends ActionSupport{
9 @Action(value = "/user/login")
10 public String execute() throws Exception {
11 if ("nvmit".equals(getName()) && "nvmit".equals(getPwd())) {
12 return "SUCCESS";
13 } else {
14 return "ERROR";
15 }
16 }
17
18 //Java Bean to hold the form parameters
19 private String name;
20 private String pwd;
21
22 public String getName() {
23 return name;
24 }
25
26 public void setName(String name) {
27 this.name = name;
28 }
29
30 public String getPwd() {
31 return pwd;
32 }
33
34 public void setPwd(String pwd) {
35 this.pwd = pwd;
36 }
37 }
38
The above class can be accessed by @Namespace + @Action that is /user/form
http://localhost:8080/Struts2Demo-1/user/form
Create Project with beloved structure
Project Structure
user/login.jsp
1 <%@ taglib uri="/struts-tags" prefix="s"%>
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
5 <title>Login Page</title>
6 </head>
7 <body>
8 <h3>Welcome User, please login below</h3>
9 <s:form action="/user/login">
10 <s:textfield name="name" label="User Name"></s:textfield>
11 <s:textfield name="pwd" label="Password" type="password"></s:textfield>
12 <s:submit value="Login"></s:submit>
13 </s:form>
14 </body>
15 </html>
user/index.jsp
1 <%@page contentType="text/html" pageEncoding="UTF-8"%>
2 <%@taglib prefix="s" uri="/struts-tags" %>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>JSP Page</title>
8 </head>
9 <body>
10 <h1>Hello <s:property value="name" /></h1>
11 </body>
12 </html>
account.jsp
1 <%@page contentType="text/html" pageEncoding="UTF-8"%>
2 <%@taglib prefix="s" uri="/struts-tags" %>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>JSP Page</title>
8 </head>
9 <body>
10 <h1>Hello <s:property value="name" /></h1>
11 </body>
12 </html>
13
Index.jsp same above
Download source code ===> here
No comments:
Post a Comment