您的位置:首页 > 编程语言 > Java开发

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



<%@ 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结构:



右击项目在服务器上运行:






运行效果:











内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Struts2 MyEclipse taglib
相关文章推荐