Java web struct入門基礎知識


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)項目結構圖:

 
 轉載 先碼后學


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM