• 【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 {
    
        }
    }
    View Code
    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) {
    
        }
    }
    View Code
    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 = "";
    
    }
    View Code
    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;
        }
    }
    View Code

    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>
    View Code

    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>
    View Code

    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>
    View Code
  • 相关阅读:
    Object.defineProperty实现数据绑定
    trigger回调方法的实现
    window.print()打印页面指定内容(使用iframe保证原页面不失效)
    Visual Studio Code 快捷键大全(Windows)
    个人博客-逐梦博客,宋
    Vue.js项目在apache服务器部署后,刷新404的问题
    阿里云ECS服务器Ubuntu配置MySQL远程访问
    最全面的css布局
    js中数组常用方法总结
    PHP环境搭建
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/12309725.html
Copyright © 2020-2023  润新知