Tải bản đầy đủ (.doc) (284 trang)

Struts2 tutorials

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.03 MB, 284 trang )

Struts 2
1.Writing a application first.
We create a application use struts 2,with library struts 2.3.1.1 . Structure of project
as folowing:
Note:We need addition library struts into Web deployment assembly :
File struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" />
<struts>
<package name="default" namespace="/tutorials" extends="struts-
default">
<action name="getTutorial"
class="org.koushik.javabrains.action.TutorialAction">
<result name="success">/success.jsp</result>
<result name="failure">/error.jsp</result>
</action>
</package>
</struts>
Class TutorialAction :
package org.koushik.javabrains.action;
public class TutorialAction {
public String execute(){
System.out.println("hello from execute");
return "success";
}
}
File web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="


xmlns="
xmlns:web="
xsi:schemaLocation="
id="WebApp_ID"
version="3.0">
<display-name>Struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts</filter-name>
<filter-
class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteF
ilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
Besides,we have two file is success.jsp and error.jsp ,index.jsp.
Kết quả :

Conclusion :
Web application look up the execute method in the package at somewhere and
execution this method.
When input data ,strust file will define action in the class have execute method ,
execute method return a value,struts file based on this value to show a jsp page
certain.
2.Accessing input parameter.
Project is same above.We have example folowing :
Example 1:
Class TutorialFinderServer :
package org.koushik.javabrains.server;
public class TutorialFinderServer {
public String getTutorialSite(String language){
if(language.toLowerCase().equals("java")){
return "Java brain";
}
else{
return "Language not supported yet";
}
}
}
Class TutorialAction :
package org.koushik.javabrains.action;
import org.koushik.javabrains.server.TutorialFinderServer;
public class TutorialAction {
private String bestTutorialSite;
private String language;
public String execute(){
TutorialFinderServer tutorialFinderServer=new
TutorialFinderServer();

bestTutorialSite=tutorialFinderServer.getTutorialSite(language);
System.out.println(bestTutorialSite);
return "success";
}
public String getBestTutorialSite() {
return bestTutorialSite;
}
public void setBestTutorialSite(String bestTutorialSite) {
this.bestTutorialSite = bestTutorialSite;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
Kết quả :
We can apply to get input of textfield in jsp.
Example 2:
Class WelcomeUseAction:
package com.mkyong.user.action;
public class WelcomeUserAction{
private String username;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;

}
// all struts logic here
public String execute() {
return "SUCCESS";
}
}
struts.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" />
<struts>
<package name="user" namespace="/User" extends="struts-default">
<action name="Login">
<result>pages/login.jsp</result>
</action>
<action name="Welcome"
class="com.mkyong.user.action.WelcomeUserAction">
<result name="SUCCESS">pages/welcome_user.jsp</result>
</action>
</package>
</struts>
login.jsp page :
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 Hello World Example</h1>

<s:form action="Welcome">
<p><s:textfield name="username" label="Username"/></p>
<s:password name="password" label="Password"/>
<s:submit/>
</s:form>
</body>
</html>
welcome_user.jsp page :
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<style type="text/css">
h4 {
color: red;
}
</style>
</head>
<body>
<h1>Struts 2 Hello World Example</h1>
<h4>Hello <s:property value="username"/></h4>
</body>
</html>
Result:
When click into submit then we have result as folowing :
Struts 2
1.Call xml file in struts.xml file
<include file="login.xml"></include>
NOTE :login.xml is a file have syntax same struts.xml file.
2.Property tag

Struts 2 Property Tag Example

Posted on July 12, 2010
By mkyong
Download It – Struts2-Property-Tag-Example.zip
Struts 2 “property” tag is used to get the property value from a class, which will default to the current
Action class (top of the stack) property if none is specified. In this tutorial, it shows the use of
“property” tag to get the property value from the current Action class and other bean class.
1. Action
An Action class, with a “name” property.
PropertyTagAction.java
package com.mkyong.common.action;

import com.opensymphony.xwork2.ActionSupport;
public class PropertyTagAction extends ActionSupport{

private String name = "Name from PropertyTagAction.java";

public String getName() {
return name;
}

public String execute() throws Exception {

return SUCCESS;
}
}
2. Bean
A simple Java class, with a “name” property.
Person.java

package com.mkyong.common;

public class Person {

private String name = "Name from Person.java";

public String getName() {
return name;
}

}
3. property tag example
It shows the use of “property” tag to get the “name” property value from the “PropertyTagAction”
and “Person” class.
property.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>Struts 2 property tag example</h1>

<h4>1. Call getName() from propertyTagAction.java</h4>
<s:property value="name" />

<h4>2. Call getName() from Person.java</h4>
<s:bean name="com.mkyong.common.Person" var="personBean" />
<s:property value="#personBean.name" />


</body>
</html>
The “property.jsp” page is a success result page returned by the “PropertyTagAction” action. If
you specified a<s:property value=”name” /> in “property.jsp” page, it will default to the current
Action class “PropertyTagAction.getName()” property.
4. struts.xml
Link it ~
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" />
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="propertyTagAction"
class="com.mkyong.common.action.PropertyTagAction" >
<result name="success">pages/property.jsp</result>
</action>

</package>
</struts>
5. Demo
http://localhost:8080/Struts2Example/propertyTagAction.action
Output
3.Action wildcards. (các ký kiệu hành động)
4.Basic example .
ActionLogin.class
package org.koushik.javabrains.action;
import com.opensymphony.xwork2.ActionSupport;
public class ActionLogin extends ActionSupport{

private String username="";
private String password="";
@Override
public String execute() throws Exception {
if(getUsername().toLowerCase().equals("quangthao") &&
getPassword().toLowerCase().equals("123456")){
return SUCCESS;
}
return LOGIN;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
login.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" />
<struts>
<package name="default" namespace="/tutorials" extends="struts-

default">
<action name="Login" >
<result>/pages/login.jsp</result>
</action>
<action name="ActionLogin"
class="org.koushik.javabrains.action.ActionLogin">
<result name="success">/pages/welcome.jsp</result>
<result name="login">/pages/login.jsp</result>
</action>
</package>
</struts>
struts.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" />
<struts>
<include file="login.xml"></include>
</struts>
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
" /><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<s:form action="ActionLogin">
<s:textfield name="username" label="Username "></s:textfield>
<s:password name="password" label="Password "></s:password>
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
" /><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome<br/>
username :<s:property value="username"/>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="
xmlns="
xsi:schemaLocation="
id="WebApp_ID"
version="3.0">
<display-name>Struts2Tutorial</display-name>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-
class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</
filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
Kết quả
5. Struts 2 Namespace Configuration Example And Explanation
Namespace configuration to avoid conflicts (tránh mâu thuẫn,tránh tranh chấp)
between same action names.
Example:
Folder structure.
struts-namespace-demo.xml file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" />
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="SayWelcome">
<result>pages/SayWelcome.jsp</result>
</action>
</package>
<package name="common" namespace="/common" extends="struts-default">
<action name="SayWelcome">
<result>pages/SayWelcome.jsp</result>
</action>
</package>
<package name="user" namespace="/user" extends="struts-default">
<action name="SayWelcome">
<result>pages/SayWelcome.jsp</result>
</action>
</package>
</struts>
Note: package name have to different.
struts.xml file.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" />
<struts>
<include file="struts-namespace-demo.xml"></include>
</struts>
WebContent/SayWelcome.jsp , WebContent/common/SayWelcome.jsp and
WebContent/user/SayWelcome.jsp have content respectively is Welcome - namespace =

"root",Welcome - namespace = "common" and Welcome - namespace = "user".
Result :
Reference
Struts 2 Namespace Configuration Example And
Explanation
Posted on June 7, 2010 , Last modified : August 29, 2012
By mkyong
Struts 2 Namespace is a new concept to handle the multiple modules by given a namespace to
each module. In addition, it can used to avoid conflicts between same action names located at
different modules.
Download It – Struts2-NameSpace-Configuration-Example.zip
Struts 2 Namespaces are the equivalent of Struts 1 multiple modules
See this picture to understand how a URL match to Struts 2 action namespace.
1. Namespace configuration
Let go through a Struts 2 namescape configuration example to know how it match with URL and
folder.
P.S The package “name” will not affect the result, just give a meaningful name.
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" />
<struts>

<package name="default" namespace="/" extends="struts-default">
<action name="SayWelcome">
<result>pages/welcome.jsp</result>
</action>
</package>


<package name="common" namespace="/common" extends="struts-default">
<action name="SayWelcome">
<result>pages/welcome.jsp</result>
</action>
</package>

<package name="user" namespace="/user" extends="struts-default">
<action name="SayWelcome">
<result>pages/welcome.jsp</result>
</action>
</package>

</struts>
Struts 2 action namespace map to folder structure.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×