【Struts 分派Action】DispatchAction

LoginAction
package k.action;

import k.form.UserForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginAction extends DispatchAction {

    public ActionForward userLogin(ActionMapping mapping, ActionForm form,
                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
        return mapping.findForward("login");
    }

    public ActionForward doUserLogin(ActionMapping mapping, ActionForm form,
                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
        UserForm userForm = (UserForm) form;
        if ("1".equals(userForm.getPassword())) {
            return mapping.findForward("ok");
        } else {
            return mapping.findForward("err");
        }
    }

    public ActionForward userLoginOut(ActionMapping mapping, ActionForm form,
                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().invalidate();
        System.out.println("userLoginOut");
        return mapping.findForward("login");
    }
    public ActionForward userLoginOut2(ActionMapping mapping, ActionForm form,
                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().invalidate();
        System.out.println("userLoginOut2");
        return mapping.findForward("login");
    }
}

struts-config.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
        "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
    <form-beans>
        <form-bean name="userForm" type="k.form.UserForm"></form-bean>
    </form-beans>
    <action-mappings>
        <action name="userForm" path="/login" parameter="action" type="k.action.LoginAction"
                scope="request" attribute="userForm" input="index.jsp" validate="false">
            <forward name="ok" path="/WEB-INF/jsp/ok.jsp"></forward>
            <forward name="err" path="/WEB-INF/jsp/err.jsp"></forward>
            <forward name="login" path="/WEB-INF/jsp/login.jsp"></forward>
        </action>
    </action-mappings>
</struts-config>

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登录页面</h1>
<form action="${APP_PATH}/login.do?action=doUserLogin" method="post">
    账号:<input type="text" name="userName" value="11哈哈"> <br>
    密码: <input type="password" name="password" value="1"> <br>
    <input type="submit" value="submit"> <br>
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>k.filter.EncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <display-name>StartSystemListener</display-name>
        <listener-class>k.filter.StartSystemListener</listener-class>
    </listener>
</web-app>
EncodingFilter

package k.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;

public class EncodingFilter extends HttpServlet implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        // System.out.println("========== set utf-8 ok ==========");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
}

StartSystemListener

package k.filter;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class StartSystemListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //1.将项目上下文路径(request.getContextPath())放置到application域中.
        ServletContext application = sce.getServletContext();
        String app_path = application.getContextPath();
        application.setAttribute("APP_PATH", app_path);
        System.out.println("========== APP_PATH = " + app_path);
        WebHelper.setApp_Path(app_path);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

WebHelper

package k.filter;

public class WebHelper {

    public static String getApp_Path() {
        return APP_PATH;
    }

    public static void setApp_Path(String appPath) {
        APP_PATH = appPath;
    }

    private static String APP_PATH = "";

}

UserForm

package k.form;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {
private String name;
    private String password;

    public UserForm() {
    }

    public UserForm(String name, String password) {

        this.name = name;
        this.password = password;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

err.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登录失败</h1>
<a href="${APP_PATH}/login.do?action=userLogin">返回登录</a>
</body>
</html>

ok.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登录失败</h1>
<a href="${APP_PATH}/login.do?action=userLogin">返回登录</a>
</body>
</html>

index,jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
<jsp:forward page="WEB-INF/jsp/login.jsp"></jsp:forward>
  </body>
</html>

原文地址:https://www.cnblogs.com/kikyoqiang/p/12309725.html

时间: 2024-11-02 12:08:20

【Struts 分派Action】DispatchAction的相关文章

实现Spring管理struts的Action

struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean. 可以在struts2中配置: <struts> <constant name="struts.objectFactory" value="spring" /> </struts> 同时action的配置class='beanID',访问该Action时,会通过class对应值去spring中寻找相同id值的

Struts中Action&ActionForm

在Struts架构中,通常使用一种名为ActionForm的系统状态Bean,来实现应用系统的非持久性数据存储和维护功能.具体来说,这种类型的对象主要用于保存用户请求表单中的数据,并可保持其状态的连续性,即在不同的页面间传递这些数据. ActionForm Bean的运行处理过程如下: 控制器ActionServlet接收到一个客户端请求后,会将该请求委托给一个RequestProcessor对象进行处理.该对象是遵照配置文件struts-config.xml中与该请求匹配的<action>子

struts中action名称重复导致的奇异事件

最近由于项目需求变更,需要本人对其中的某个业务功能进行修改.本人按照前台页面找action,根据action找代码的逻辑进行了修改(公司项目是ssh框架,struts配置全部是通过注解的方式进行,配置简单方便).当然测试人员也成功的进行了测试,发现没有任何问题,成功发版.奇葩事情来了,在发版环境中,修改的代码总是没用! 没办法,问题还是要解决,在确认了发版环境的确是最新代码之后,回自己座位找原因.这次我用action名称全局搜索项目工程,尼玛发现两个重名action,当然我只修改了其中一个文件,

(五)Struts之Action类基础(二)

上一章节末((三)Struts之Action类基础(一))介绍了如何获取用户输入数据的获取.接着就是在Struts中怎么把数据响应给用户端,这就必须要求我们把数据放到作用域中,然后才能显示到用户浏览器. 一.将数据放到作用域并在用户浏览器中显示 A. 使用Servlet原生作用域(request.session.servletContext) index.jsp <%@ page language="java" contentType="text/html; chars

重读《Struts In Action》

Figure   1.1. The Java Servlet API exposes the HTTP client/server protocol to the Java   platform. Struts 2 is built on top of that.   For web applications, HTTP has two hurdles to get over. It’s stateless, and it’s text based.   Don’t reinvent the w

Struts之action的请求接受参数

1.Struts2 提供三种数据封装的方式 Action 本身作为model对象,通过成员setter封装 创建独立model对象,页面通过ognl表达式封装 使用ModelDriven接口,对请求数据进行封装 Action充当的角色既可以是Model也可以使Controler. 2.Action 本身作为model对象,通过成员setter封装,而且是必须要属性的setter方法,与getter并没有关系(属性驱动方式): 3.创建独立model对象,页面通过ognl表达式封装,必须要User

解决weblogic下通过war加载jar包中的struts的action找不到的问题

今天在功能测试环境中weblogic上部署应用时,启动后报错,说是spring找不到对应的action,所以怀疑是类加载的问题,找度娘一搜,果然是猜的没错,找到解决方法: 1. 将struts.xml加入: <constant name="struts.convention.action.includeJars" value=".*你的jar包名.*" /> 然后启动正常,但登陆后找不到struts的action.在开发环境中明显是可以的,而且在tomc

Struts 1之DispatchAction

DispatchAction是struts 1 的内置通用分发器 import org.apache.struts.actions.DispatchAction; public class UserAction extends DispatchAction { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse res

struts+service+action+数据库

用户登录流程 1.jsp根据form表单中的action的login   <form action="/test02/login" method="post"> 请求struts.xml文件中的 <action name="login" class="action.LoginAction" method="add"> 2.struts.xml根据  class="actio