JavaWeb_(Struts2框架)Struts建立Action的三種方式

 

 

  此係列博文基於同一個項目已上傳至github  傳送門html

  JavaWeb_(Struts2框架)Struts建立Action的三種方式  傳送門java

  JavaWeb_(Struts2框架)struts.xml核心配置、動態方法調用、結果集的處理  傳送門git

  JavaWeb_(Struts2框架)Log4j的配置以及解決中文亂碼  傳送門github

  JavaWeb_(Struts2框架)參數傳遞之接收參數與傳遞參數  傳送門web

  JavaWeb_(Struts2框架)Ognl小案例查詢帖子  傳送門apache

  JavaWeb_(Struts2框架)Action中struts-default下result的各類轉發類型  傳送門框架

  JavaWeb_(Struts2框架)攔截器interceptor  傳送門dom

 

  

  第一種DefaultAction什麼都沒有幹jsp

  第二種實現了Action接口ide

  第三種繼承了ActionSupport類【經常使用】

 

  目錄結構

  

 

package com.Gary.web;

public class DefaultAction {

    public String execute()
    {
        System.err.println("這是一個普通的action");
        return null;
    }
    
}
DefaultAction.java

 

package com.Gary.web;

import com.opensymphony.xwork2.Action;

class ImplAction implements Action{

    @Override
    public String execute() throws Exception {
        System.out.println("這是實現了Action接口的action");
        return null;
    }

}
ImplAction.java

 

package com.Gary.web;

import org.apache.struts2.ServletActionContext;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{

    public User user = new User();
    
    public String execute() throws Exception {
        
        System.err.println("我已經運行了");
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用戶名或密碼錯誤!!!");
            return "error";
        }
        
    }

    
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }
}
UserAction.java

 

  配置struts.xml

<struts>
    <!-- name:配置包名 -->
    <package name="MyPackage" namespace="/" extends="struts-default">
        <action name="LoginAction" class="com.Gary.web.UserAction" method="execute">
            <!-- 默認爲轉發  redirect設置爲重定向-->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>
        
        <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute">
        </action>
        
        <action name="LoginActionImpl" class="com.Gary.web.ImplAction" method="execute"></action>
    </package>
</struts>

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    
<struts>
    <!-- name:配置包名 -->
    <package name="MyPackage" namespace="/" extends="struts-default">
        <action name="LoginAction" class="com.Gary.web.UserAction" method="execute">
            <!-- 默認爲轉發  redirect設置爲重定向-->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>
        
        <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute">
        </action>
        
        <action name="LoginActionImpl" class="com.Gary.web.ImplAction" method="execute"></action>
    </package>
</struts>
struts.xml
相關文章
相關標籤/搜索