Struts框架的入門使用

1.struts框架的使用 html

   導入jar包java

   1.commons-fileupload-1.2.jarweb

   2. freemarker-2.3.15.jarapache

   3.ognl-2.7.3.jar app

   4.struts2-core-2.1.8.jar框架

   5.xwork-core-2.1.6.jarjsp

  2.配置web.xmlui

     

  <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>

 

3.配置struts.xmlurl

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    
    
    <!-- 
    歲月能夠褪去記憶,卻褪不去一路留下的歡聲笑語。2017,但願你們快樂繼續,歲歲安怡
    
     -->
    
    <!-- 
        一、name指定包名,包與包之間的名稱不能重複;
        二、namespace包命名空間,用於拼咱們訪問包的
            路徑的,即URL。
            不要忘記/線
        三、extends指定包的繼承關係的,這樣咱們能夠
            方便的使用Struts2提供的默認配置。
     -->
    <package name="demo" 
        namespace="/hello" 
        extends="struts-default">
        <!-- 
            一、action是咱們本身要寫的業務組件,
                用於封裝業務邏輯代碼。
            二、name是指定action的名稱的,
                該名稱會用於咱們訪問action。
                同一包下能夠有多個action,
                action名不能重複。
            三、class指定該action對應的業務組件,
                即咱們本身定義的業務代碼類。
            四、method指定要訪問的方法名。
                這個method能夠省略,若省略
                則默認調用execute方法。
            五、要訪問當前的Action,其URL以下:
            http://localhost:8080/Struts2demo01/hello/hello.action
            http://ip:port/project_name/package_namespace/action_name.action
            注意:action名稱的後綴.action能夠省略的
         -->
        <action name="hello" 
            class="demo.HelloAction" 
            method="sayHello">
            <!-- 
                一、用result指定action處理完請求以後,
                    要去向的頁面。
                二、name指定result的名稱,
                    用於訪問該result,
                    同一個action下能夠有多個result,
                    他們之間不能重名。
             -->
            <result name="success">
                /WEB-INF/jsp/helloStruts.jsp
            </result>
            <result name="error">
                /WEB-INF/jsp/error.jsp
            </result>
        </action>
    </package>
    
</struts>

  4.寫error.jsp 以及helloStruts.jspspa

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  This is a Error!
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  Hello World!
</body>
</html>

  5.寫HelloAction這個java類

package demo;

public class HelloAction {

    /**
     * 該方法在struts.xml配置過, 與配置文件相對應。
     */
    public String sayHello() {
        System.out.println("Hello,Struts!");
        /*
         * 返回的字符串與struts.xml中 定義的result對應,是用來找result的。
         */
        return "success";
    }
    
}

  運行

相關文章
相關標籤/搜索