JavaWeb_(Struts2框架)Struts创建Action的三种方式

 

 

  此系列博文基于同一个项目已上传至github  传送门

  JavaWeb_(Struts2框架)Struts创建Action的三种方式  传送门

  JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理  传送门

  JavaWeb_(Struts2框架)Log4j的配置以及解决中文乱码  传送门

  JavaWeb_(Struts2框架)参数传递之接收参数与传递参数  传送门

  JavaWeb_(Struts2框架)Ognl小案例查询帖子  传送门

  JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型  传送门

  JavaWeb_(Struts2框架)拦截器interceptor  传送门

 

  

  第一种DefaultAction什么都没有干

  第二种实现了Action接口

  第三种继承了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

 

posted @ 2019-03-05 20:08  Cynical丶Gary  阅读(628)  评论(0编辑  收藏  举报