MyEclipse struts2 拦截器处理用户请求简单实例
2017-11-11 15:54
375 查看
Struts2简介
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是在
struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts
1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts
2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。
拦截器处理用户请求实例:
①在myeclipse新建一个web project
②配置struts2:
右击项目 - MyEclipse - Project Facets[Capabilities] - install apache struts(2.x) Facet
③:新建login页面,@taglib prefix="s" uri="/struts-tags"引入struts2标签库,s为标签库的别名,这样在使用时就不用每次都要把较长的标签库名写出来,方便使用。uri为本地标签库的地址。
login.jsp
action为处理该form表单的servlet的struts.xml配置的值
④:创建servlet处理该请求:user.class
user.class 可以自动生成该javaBean模型,具体步骤看上一篇博客:MyEclipse自动生成JavaBean
struts.xml
package name="use"为该package命名,随便起个名
action name="dis"对应form表单的action名称,class为处理该action用户请求的servlet
[b]命名空间namespace="/test"与动作名称action
name="dis"用来处理form表单action="/test/dis.action"的请求
[/b]
result为servlet的execute()函数返回的值对应的处理方式,例:当返回值为ok时跳转到welcome.jsp页面
⑥:新建welcome.jsp页面
⑦:配置web.xml文件
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
添加后缀名为jsp的文件使用struts过滤
<dispatcher> :配置到达servlet的方式,可以同时配置多个。有四种取值:REQUEST、FORWARD、ERROR、INCLUDE。如果没有配置,则默认为REQUEST。它们的区别是:
# REQUEST :表示仅当直接请求servlet时才生效。
# FORWARD :表示仅当某servlet通过forward转发到该servlet时才生效。
# INCLUDE :Jsp中可以通过<jsp:include/>请求某servlet, 只有这种情况才有效。
# ERROR :Jsp中可以通过<%@page errorPage="error.jsp" %>指定错误处理页面,仅在这种情况下才生效。
⑧:web project结构:
右击项目在服务器上运行:
运行效果:
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是在
struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts
1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts
2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。
拦截器处理用户请求实例:
①在myeclipse新建一个web project
②配置struts2:
右击项目 - MyEclipse - Project Facets[Capabilities] - install apache struts(2.x) Facet
③:新建login页面,@taglib prefix="s" uri="/struts-tags"引入struts2标签库,s为标签库的别名,这样在使用时就不用每次都要把较长的标签库名写出来,方便使用。uri为本地标签库的地址。
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action="/test/dis.action"> <s:textfield name="username" label="用户名"></s:textfield> <s:password name="password" label="密码"></s:password> <s:submit></s:submit> </s:form> </body> </html>s:form为struts2标签库定义from表单,action="/test/dis.action"发出的请求为http://localhost:8080/struts2test/test/dis.action对应struts.xml的package namespace(test)以及action的name(dis),这样就能找到对应的action来处理用户请求
action为处理该form表单的servlet的struts.xml配置的值
④:创建servlet处理该请求:user.class
user.class 可以自动生成该javaBean模型,具体步骤看上一篇博客:MyEclipse自动生成JavaBean
package com.struts.user.action; public class user { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String execute(){ return "ok"; } }⑤:将该user.class Servlet配置到struts.xml内,当用户请求被拦截器拦下可以在struts.xml内通过form表单的action="dis"找到对应的action的servlet处理该请求
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="use" namespace="/test" extends="struts-default"> <action name="dis" class="com.struts.user.action.user"> <result name="ok">/welcome.jsp</result> </action> </package> </struts>
package name="use"为该package命名,随便起个名
action name="dis"对应form表单的action名称,class为处理该action用户请求的servlet
[b]命名空间namespace="/test"与动作名称action
name="dis"用来处理form表单action="/test/dis.action"的请求
[/b]
result为servlet的execute()函数返回的值对应的处理方式,例:当返回值为ok时跳转到welcome.jsp页面
⑥:新建welcome.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>Hello,<s:property value="username"/></h1> </body> </html>S:property name="username"取得参数为username的值
⑦:配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>struts2test</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher></filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list></web-app>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
添加后缀名为jsp的文件使用struts过滤
<dispatcher> :配置到达servlet的方式,可以同时配置多个。有四种取值:REQUEST、FORWARD、ERROR、INCLUDE。如果没有配置,则默认为REQUEST。它们的区别是:
# REQUEST :表示仅当直接请求servlet时才生效。
# FORWARD :表示仅当某servlet通过forward转发到该servlet时才生效。
# INCLUDE :Jsp中可以通过<jsp:include/>请求某servlet, 只有这种情况才有效。
# ERROR :Jsp中可以通过<%@page errorPage="error.jsp" %>指定错误处理页面,仅在这种情况下才生效。
⑧:web project结构:
右击项目在服务器上运行:
运行效果:
相关文章推荐
- Spring MVC代码实例系列-11:Spring MVC实现简单的权限控制拦截器和请求信息统计拦截器
- 简单的Struts2的Interceptors(拦截器)机制介绍以及利用拦截器实现用户登陆时执行耗时操作并自动跳转的实例
- 一个处理用户登陆的servlet简单实例
- 一个处理用户登陆的servlet简单实例
- 简单的拦截器请求头
- BT源代码学习心得(八):跟踪服务器(Tracker)的代码分析(用户请求的实际处理) - 转贴自 wolfenstein (NeverSayNever)
- java model2用户登录简单实例
- 4.构造方法与重载:定义一个网络用户类,要处理的信息有用户 ID、用户密码、 email 地址。在建立类的实例时把以上三个信息都作为构造函数的参数输入, 其中用户 ID 和用户密码时必须缺省时 ema
- JSP+JavaBean+Servlet用户注册登录请求的处理
- Struts2 拦截器处理普通Http请求和Ajax请求时拦截配置
- java网络编程二:服务器处理多个用户请求的解决方案(返回多个用户输入的信息)
- 最简单的cmd批处理入门实例
- 【转载】QPS,用户平均等待时间,服务器平均请求处理时间
- 【Swift】 GET&POST请求 网络缓存的简单处理
- springMVC一个Controller处理所有用户请求的并发问题
- tomcat请求处理分析(一) 启动container实例
- 采用JSP+JavaBean的方式进行简单的实现用户的网页登陆实例
- 利用紧急救援和单用户模式处理简单异常
- 图像处理之简单综合实例(大米计数)
- Struts2 拦截器处理普通Http请求和Ajax请求时拦截配置