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>

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

  1. 重读《Struts In Action》

    Figure   1.1. The Java Servlet API exposes the HTTP client/server protocol to the Java   platform. S ...

  2. 关于Spring的Controller及Struts的Action的多线程的注意

    struts是线程安全,并不是指多线程,而是指单态,当多个用户访问一个请求的时候,服务器内存中只有一个与之对应的action类对象,execute方法加上了同步关键字,如果你在action里加上一个全 ...

  3. struts中action名称反复导致的神秘事件

    近期由于项目需求变更.须要本人对当中的某个业务功能进行改动.本人依照前台页面找action,依据action找代码的逻辑进行了改动(公司项目是ssh框架,struts配置全部是通过注解的方式进行.配置 ...

  4. 实现Spring管理struts的Action

    struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean.  可以在struts2中配置:  <struts>      ...

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

    上一章节末((三)Struts之Action类基础(一))介绍了如何获取用户输入数据的获取.接着就是在Struts中怎么把数据响应给用户端,这就必须要求我们把数据放到作用域中,然后才能显示到用户浏览器 ...

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

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  7. Struts 1之DispatchAction

    DispatchAction是struts 1 的内置通用分发器 import org.apache.struts.actions.DispatchAction; public class UserA ...

  8. Struts中Action三种接收参数的方式?

    前言: 前面已经有一篇随笔介绍了Struts2的大概原理.本文就Struts2中Action与jsp页面进行数据对接时介绍几种常见方法! 值栈ValueStack 3个Action Action1 p ...

  9. struts+service+action+数据库

    用户登录流程 1.jsp根据form表单中的action的login   <form action="/test02/login" method="post&quo ...

随机推荐

  1. (转)Boyer-Moore算法

    转自:Boyer-Moore算法 一.简述 在当前用于查找子字符串的算法中,BM(Boyer-Moore)算法是当前有效且应用比较广的一中算法,各种文本编辑器的“查找”功能(Ctrl+F),大多采用B ...

  2. c数据结构 -- 线性表之 复杂的链式存储结构

    复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...

  3. php设计模式之工厂方法实例代码

    实现不修改原代码,扩展新功能 <?php header("Content-type:text/html;charset=utf-8"); /** * db接口 * 实现连接数 ...

  4. 爬山 启发式合并 / STL

    题目 其实 Kano 曾经到过由乃山,当然这名字一看山主就是 Yuno 嘛.当年 Kano 看见了由乃山,内心突然涌出了一股杜甫会当凌绝顶,一览众山小的豪气,于是毅然决定登山. 但是 Kano 总是习 ...

  5. Linux - 文件时间戳

    概述 简介 linux 文件时间戳 背景 最近感觉很消极的样子 心情不好加不知道写啥 随便水一水 能水的就那么多, 水一次, 少一次 环境 os centos7 1. 时间戳 概述 简述 时间戳 li ...

  6. AAC huffman decoding

    在AAC编码器内部,使用huffman coding用于进一步减少scalefactor和量化频谱系数的冗余. 从individual_channel_stream层提取码流进行huffman解码,码 ...

  7. 基于SILVACO ATLAS的a-IGZO薄膜晶体管二维器件仿真(01)

    最近因为肺炎的缘故,宅在家里不能出门,就翻了下一些资料,刚好研究方向是这个,就简单研究了下.参考资料主要如下: 1.<半导体工艺和器件仿真软件Silvaco TCAD实用教程> 唐龙谷 2 ...

  8. 问题总结:mysql和javaweb工程连接的过程中容易产生的问题

    问题背景:自己在本机的mysql8瘫痪了,将Oracle中的数据迁移到mysql之后,配置好javaweb工程和虚拟机上的远程Mysql连接的文件之后:遇见了无法访问的问题 具体的配置: dataso ...

  9. Mysql SQL CAST()函数

    (1).CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型.以下例子用于将文本字符串'12'转换为整型: SELECT CAST('12' AS int) (2).返回值是 ...

  10. IntelliJ IDEA 2017.3尚硅谷-----卸载

    直接在用户目录下搜索,卸载的干净就要删除 删除这两个目录,重启idea可以还原配置. editplus删除后重启也是这个效果