struts學習之入門級

學了spring mvc的一些東西,下面是我學習的struts結構,都用反射來做,其實感覺有的地方相通。

下面是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_2_5.xsd" version="2.5">
  <display-name></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>
下面是action類的struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <package name="struts2" extends="struts-default">
            <action name="HelloWorld" class="tutorial.HelloWorld">
                <result>/HelloWorld.jsp</result>
            </action>
        </package>
    </struts>

當然必須有處理action請求的類

public class HelloWorld extends ActionSupport  
{  
    public final static String MESSAGE = "Struts2 is up and running ...";  
      
    private String message;  
  
  
    /** 
     * @return the message 
     */  
    public String getMessage()  
    {  
        return message;  
    }  
  
  
    /** 
     * @param message the message to set 
     */  
    public void setMessage(String message)  
    {  
        this.message = message;  
    }  
  
  
    public String execute() throws Exception  
    {  
    	System.out.println("aaaaaaaaaaaaaaaaaa");
        setMessage(MESSAGE);  
        return SUCCESS;  
    }  
}  
最後就是jsp頁面了。

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>    
    <title>Hello World!</title>    
  </head>
  
 <body>
     <form action="HelloWorld"><!-- 提交到action,也就是struts.xml的對應類 -->
     <input type="submit" value="提交" />
     </form>
 </body>
</html>
HelloWorld.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>    
    <title>Hello World!</title>    
  </head>
  
  <body>
    <h2><s:property value="message" /></h2>
  </body>
</html>
我們登錄:127.0.0.1:8080/Struts2Demo/index.jsp,點擊提交就到HelloWorld.jsp
這個很簡單,就不多說什麼啦。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章