前言2:struts2 架構
Struts2 的 Action 能夠相容 POJO
講白話一點就是什麼都不用繼承的 class 就能當 Action 用.
POJO類別簡單來說,就是單純的
Java類別,不帶有其他框架 API 呼叫,開發者可以以簡單不複雜的方式(不必實
作一些繁複的介面或繼承)來開發商業邏輯,減少與其他元件的耦合性.
action:Each URL is mapped to a specific action.
POJO類別簡單來說,就是單純的
Java類別,不帶有其他框架 API 呼叫,開發者可以以簡單不複雜的方式(不必實
作一些繁複的介面或繼承)來開發商業邏輯,減少與其他元件的耦合性.
action:Each URL is mapped to a specific action.
2. create dynamic web project
3. put structs2 lib to your web-inf\lib
4. create model =>HelloWorldAction.java
Struts 2 framework will create an object of HelloWorldAction.java,
and call execute method.
package com.tutorialspoint.struts2; public class HelloWorldAction{ private String name; private String age; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String execute() throws Exception { System.out.println(name+age); return "success2"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
5. create view under webContent=>HelloWorld.jsp
<s:property value="name"/> 會呼叫 HelloWorldAction.java的getname()
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html>Hello World </head> <body> Hello World,<s:property value="name"> age ,<s:property value="age"> </body> </html>
6.create main page under webContent => index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head>Hello World </head> <body>Hello World From Struts2
Hello World From Struts2 Extend
Hello World From Struts2 Map
</body> </html>
action = hello . mapping to HelloWorldAction class , use struts.xml to setting
7.create struts.xml under src/
/HelloWorld.jsp /HelloWorld.jsp /AccessDenied.jsp /HelloWorldMap.jsp /AccessDenied.jsp
The only requirement for actions in Struts2 is that there must be one no-argument method that returns either a String or Result object and must be a POJO. If the no-argument method is not specified, the default behavior is to use the execute() method.
8.create web.xml under webContent/web-inf
Struts 2 index.jsp struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /*
9.enable detail log (先跳過)
10. [test] run your project
11. struts.properties file (if need, this will have default)
### When set to true, Struts will act much more friendly for developers struts.devMode = true ### Enables reloading of internationalization files struts.i18n.reload = true ### Enables reloading of XML configuration files struts.configuration.xml.reload = true ### Sets the port that the server is run on struts.url.http.port = 8080
create under web-inf/classes
Create Multiple Actions
_1. create HelloWorldActionExtendActionSupport.java
package com.tutorialspoint.struts2; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldActionExtendActionSupport extends ActionSupport{ private String name; public String execute() throws Exception { if ("SECRET".equals(name)) { return SUCCESS; }else{ return ERROR; } } public String getName() { return name; } public void setName(String name) { this.name = name; } }
_2. add content to struts.xml
/HelloWorld.jsp /HelloWorld.jsp /AccessDenied.jsp /HelloWorldMap.jsp /AccessDenied.jsp
ValueStack/OGNL Example
The Object-Graph Navigation Language (OGNL) is a powerful expression language that is used to reference and manipulate data on the ValueStack. OGNL also helps in data transfer and type conversion.
=1. create HelloWorldActionExtendActionMap.java
package com.tutorialspoint.struts2; import java.util.*; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldActionMap extends ActionSupport{ private String name; public String execute() throws Exception { ValueStack stack = ActionContext.getContext().getValueStack(); Mapcontext = new HashMap (); context.put("key1", this.name); context.put("key2", this.name); stack.push(context); System.out.println("Size of the valueStack: " + stack.size()); return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
=2. add content to struts.xml(同上)
=3. add HolloWorldMap.jsp
=3. add HolloWorldMap.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head>Hello World </head> <body> Entered value : <s:property value="name"> Value of key 1 : <s:property value="key1"> Value of key 2 : <s:property value="key2"> </body> </html>
eclipse import step
回覆刪除.import project with dynamic web project
.set: Properties -> project facets -> runtimes -> click your tomcat