1. Trang chủ
  2. » Công Nghệ Thông Tin

Struts2 tutorials

284 959 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

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" "http://struts.apache.org/dtds/struts-2.0.dtd"> <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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 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" "http://struts.apache.org/dtds/struts-2.0.dtd"> <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 : [...]... name="SayStruts2"> pages/printStruts2.jsp Now you can access the “SayStruts2″ action class via Action URL : http://localhost:8080/Struts2Example/SayStruts2.html 2 No extension Change the action class to empty extension ... extension Example: pages/printStruts2.jsp To access the “SayStruts2″ action class, use the following URL : Action URL : http://localhost:8080/Struts2Example/SayStruts2.action Configure the action extension Struts 2 is allow to configure the action extension easily,... value=""/> pages/printStruts2.jsp Now you can access the “SayStruts2′ action class via Action URL : http://localhost:8080/Struts2Example/SayStruts2 Example: no extension struts-namespace-demo.xml file pages/welcome.jsp And display the content of webapp/common/pages/welcome.jsp Example 3 URL : http://localhost:8080/Struts2Example/user/SayWelcome.action Will match the user namespace pages/welcome.jsp ... encoding="UTF-8" ?> /pages/login.jsp /pages/welcome.jsp... struts.xml file 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 . TutorialFinderServer(); bestTutorialSite=tutorialFinderServer.getTutorialSite(language); System.out.println(bestTutorialSite); return "success"; } public String getBestTutorialSite() { return bestTutorialSite; } public void setBestTutorialSite(String bestTutorialSite) { this.bestTutorialSite. String bestTutorialSite; private String language; public String execute(){ TutorialFinderServer tutorialFinderServer=new TutorialFinderServer(); bestTutorialSite=tutorialFinderServer.getTutorialSite(language); System.out.println(bestTutorialSite); return. </welcome-file-list> <filter> <filter-name> ;struts2& lt;/filter-name> <filter- class>org.apache .struts2. dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</ filter-class>

Ngày đăng: 06/05/2014, 13:55

Xem thêm: Struts2 tutorials

TỪ KHÓA LIÊN QUAN

Mục lục

    Struts 2 Property Tag Example

    5. Struts 2 Namespace Configuration Example And Explanation

    3. Mapping – How it work?

    Enable the Strut2 development mode

    Disable the Strut2 development mode

    Configure the action extension

    Struts 2 ActionError & ActionMessage Example

    Struts 2 <s:textfield> textbox example

    Struts 2 <s:textfield> example

    Struts 2 <s:password> password example

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w