struts2 Demo

參考資料 :
http://www.cnblogs.com/yangy608/archive/2010/11/08/1871962.html
http://www.yiibai.com/struts2/struts_quick_guide.html

個人理解:
首先我們要清楚struts是一種MVC框架、
strut1和struts2有所區別。最明顯的應該是Struts2把Struts1的actionForm和action的結合使用,並且優化了execute方法,通過String就可以實現跳轉文件。

struts.xml
該配置文件的作用:配置action。
    action配置:
        name   action名    
        class  類路徑
        result:action result 頁面跳轉配置
        
(個人理解:這裏比struts1有所簡化,省去了form的配置以及action和form 的關聯配置.)

以下爲struts2的Demo工程源碼:

1.建立web工程,引入struts2包

2.建立package:action包

  2.1在action下建立LoginAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * 
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("LoginAction.execute()");
        System.out.println("name = " + name);
        if (name.equals("xiaoming")) {
            return "success";
        } else {
            return "error";
        }
    }

    private String name;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

}

3.配置文件

  3.1在scr文件夾下建立struts.xml

(這裏的package 的extends一直顯示undefined,故引入struts-default.xml文件,result文件也要記得配置好)

<之前一直報錯:警告: Could not find action or result
No result defined for action action.LoginAction and result error

解決辦法是在package加上 namespace="/"

>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <include file="struts-default.xml"></include>
    <package name="struts2" namespace="/" extends="struts-default">
        <action name="login" class="action.LoginAction">
            <result name="success">/WEB-INF/wel.jsp</result>
            <result name="error">/WEB-INF/error.jsp</result></action>
    </package>
</struts>    

  3.2web.xml:

(注意 filter-class文件,應該是說struts2.1版本以上的使用如下文件,struts2.0的是另一個文件)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    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_2_5.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>*.action</url-pattern>
  </filter-mapping></web-app>

4.編寫jsp頁面:

  4.1 編寫input-source 頁面index.jsp

<form action="login.action">
        name:<input type="text" name="name"> <input type="submit"
            value="submit">
    </form>

  4.2 編寫forward頁面 error.jsp 和 wel.jsp

<body>
    This is wel JSP page. <br>
    hello <%=request.getParameter("name") %>
  </body>

 

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