Java web struct入門基礎知識

來源:互聯網
上載者:User

標籤:apache   alt   result   sch   row   可維護性   this   nbsp   action   

1、Struts2的前身是Opensymphony的Webwork2,實際上Strut和Webwork2合并後形成Struts2。

 2、一個HelloWord樣本1)建立Web應用,所需要的Jar包為:2)配置Struts2    在web.xml檔案中配置如下:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"           version="3.0"><display-name>struts2_helloworld</display-name>    <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>          <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

 3)建立控制類HelloWorld

package com.yyq.action;import com.opensymphony.xwork2.ActionSupport;public class HelloWorld extends ActionSupport {    private String message;    @Override    public String execute() throws Exception {        message = "Hello World,Struts2";        return SUCCESS;    }    public String getMessage(){        return message;    }}

 4)建立index.jsp

<%@ page contentType="text/html;charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html>  <head>    <title><s:property value="message"/></title>  </head>  <body>        <s:property value="message"/>  </body></html>
5)配置HelloWorld。在Struts2中主要有三種方式來實現串連:使用XML檔案進行配置;使用Annotation來配置;使用CoC來約定命名。    在src目錄中建立一個名為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="action" namespace="/" extends="struts-default">        <action name="HWAction" class="com.yyq.action.HelloWorld">            <result>/index.jsp</result>        </action>    </package></struts>

 6)啟動Tomcat,發布並運行HelloWorld,輸入:http://localhost:8080/HWAction.action

7)項目結構:    Struts2是一個架構,架構本身由於增加了更多的分層,因此會增加軟體的複雜度,比起用純Servlet或JSP的成本高。但是分層能夠使得各個模組之間變得更清晰,適合多人協作來完成開發。並且使用分層結構,可以使軟體開發的分工趨於細緻,可以增強軟體的可讀性和可維護性。在引入Struts2時,只需要低廉的學習成本就可以得到一個高效的開發架構,這無論開發人員還是管理者的角度來說,都是非常重要的。使用Struts2並不意味著開發人員可以很輕鬆地進行開發,不用寫複雜的代碼,Struts2並不意味著開發人員可以很輕鬆地進行開發,不用再寫複雜的代碼。Struts2隻是屏蔽了分層,但複雜的商務邏輯還是需要由開發人員親自編寫。

 

3、一個簡單的使用者登入樣本

1)建立使用者POJO類檔案:
package com.yyq.pojo;public class User {    private String userName;    private String password;    public User() {    }    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;    }}

 2)建立Action類檔案:

package com.yyq.action;import com.opensymphony.xwork2.ActionSupport;import com.yyq.pojo.User;public class Login extends ActionSupport {    private User user;    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }    //執行登入的方法    public String execute(){        return SUCCESS;    }}

3)建立登陸頁面index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>登入執行個體</title></head><body>請輸入使用者名稱和密碼:<br/><form action="Login.action" method="post">    <p>        使用者名稱:<input type="text" name="user.userName"/>    </p>    <p>        密碼:<input type="text" name="user.password"/>    </p>    <input type="submit" value="登入"/></form></body></html>

4)建立登陸成功頁面welcome.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>登陸成功</title></head><body>    <p>        您的使用者名稱為:<s:property value="user.userName"/>    </p>    <p>        您的密碼為:<s:property value="user.password"/>    </p></body></html>

5)配置web.xml檔案:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"           version="3.0">    <display-name>struts2_helloworld</display-name>    <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>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

6)配置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="action" namespace="/" extends="struts-default">        <action name="Login" class="com.yyq.action.Login">            <result>/welcome.jsp</result>        </action>    </package></struts>

7)啟動Tomcat,輸入http://localhost:8080/index.jsp

 

隨意輸入使用者名稱和密碼,點擊登入即可:

8)項目結構圖:

  轉載 先碼後學

Java web struct入門基礎知識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.