java web mvc-02-struts2 入门介绍

拓展阅读

Java Servlet 教程-20-自己手写实现 spring mvc 整体思路

Java Servlet 教程-21-自己手写 spring mvc 简单实现

Spring Web MVC-00-重学 mvc

mvc-01-Model-View-Controller 概览

mvc-02-structs 介绍

web mvc-03-JFinal

web mvc-04-Apache Wicket

web mvc-05-JSF JavaServer Faces

web mvc-06-play framework intro

web mvc-07-Vaadin

web mvc-08-Grails

从零手写组件系列

java 从零手写 spring ioc 控制反转

java 从零手写 spring mvc

java 从零手写 jdbc-pool 数据库连接池

java 从零手写 mybatis

java 从零手写 hibernate

java 从零手写 rpc 远程调用

java 从零手写 mq 消息组件

java 从零手写 cache 缓存

java 从零手写 nginx4j

java 从零手写 tomcat

Struts2

Apache Struts是一个用于创建优雅、现代Java Web应用程序的免费、开源的MVC框架。

struts

Hello World

我的示例基于mavenstruts2文档。 开始使用Struts2原型的推荐方式是使用原型目录。

mvn archetype:generate -DarchetypeCatalog=http://struts.apache.org/

Struts 2空白原型("blank-archetype")提供了一个最小但完整的Struts 2应用程序。 它演示了一些最基本的Struts 2概念。这是我的风格,我选择了这个。

使用

mvn clean install

来删除先前的文件并将JAR文件添加到您的存储库。

使用

mvn tomcat7:run

启动项目。或者将WAR文件放到Tomcat中。

您可能会遇到类似以下错误。

no plugin found for prefix 'tomcat 7' in the current project and in the plugin groups

别担心,这是解决方法。打开pom.xml,将以下代码添加到plugins中。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>${plugin.tomcat.version}</version>
    <configuration>
        <port>8080</port>
        <path>/</path>
        <uriEncoding>${project.build.sourceEncoding}</uriEncoding>
    </configuration>
</plugin>

并将此代码添加到properties中。

<plugin.tomcat.version>2.2</plugin.tomcat.version>

好了,享受Struts2之旅。

Configuration Files

web.xml

web.xml文件中,Struts定义了它的FilterDispatcher,这是一个Servlet过滤器类,用于初始化Struts框架并处理所有请求。如下所示...

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

在上面的示例中,我们将Struts 2调度程序映射到/*,因此Struts 2会处理所有传入的请求。这是因为Struts 2从其JAR文件中提供静态内容,包括Dojo JavaScript文件(如果使用S2.0或S2.1+中的Dojo插件)和用于生成HTML的Struts 2标签的FreeMarker模板。

自Struts 2.1.7以来,您可以提供一个逗号分隔的模式列表,当匹配请求URL时,过滤器将直接通过。这是通过配置选项struts.action.excludePattern完成的,例如在您的struts.xml中:

<struts>
    <constant name="struts.action.excludePattern" value=".*unfiltered.*,.*\\.nofilter"/>
    ...
</struts>

您可能会问,struts.xml是什么?

struts.xml

该框架的核心配置文件是默认的(struts.xml)文件,应存放在Web应用程序的类路径上(通常是/WEB-INF/classes)。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
</struts>

DTD是文档类型定义,定义了XML文档的结构以及合法的元素和属性。

DTD

我们可以将一个大的struts.xml文件分解成较小的部分吗?

  • 您可以在struts.xml中的<package>元素中交替使用<include>元素。配置对象将按出现的顺序加载。框架从上到下读取配置并在引用时按顺序添加对象。
<struts>
    <include file="example.xml"/>
    ...
</struts>

您可能会在struts.xml中找到这些,这是常量。

<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>

常量提供了一种通过定义修改框架和插件行为的关键设置来自定义Struts应用程序的简单方法。

<package name="default" namespace="/" extends="struts-default">

    <default-action-ref name="index"/>

    <action name="index">
        <result>/index.html</result>
    </action>

</package>
  • package元素具有一个必需的属性,即name,它充当稍后引用包的键。

  • namespace属性将动作配置细分为逻辑模块,每个模块都有其自己的标识前缀。命名空间避免了动作名称之间的冲突。

  • extends属性是可选的,允许一个包继承一个或多个先前包的配置。

  • action映射可以指定一组结果类型、一组异常处理程序和一个拦截器堆栈。

struts.properties

所有属性也可以在XML配置文件中使用Constant Configuration进行设置。

属性列表可以在struts-default.properties(在struts2.jar中)中找到。

### Struts default properties
###(can be overridden by a struts.properties file in the root of the classpath)
###
### This can be used to set your default locale and encoding scheme
# struts.locale=en_US
struts.i18n.encoding=UTF-8
 
### Used by the DefaultActionMapper
### You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do
### The blank extension allows you to match directory listings as well as pure action names
### without interfering with static resources, which can be specified as an empty string
### prior to a comma e.g. struts.action.extension=, or struts.action.extension=x,y,z,,
struts.action.extension=action,,
 
### Set this to false if you wish to disable implicit dynamic method invocation
### via the URL request. This includes URLs like foo!bar.action, as well as params
### like method:bar (but not action:foo).
### An alternative to implicit dynamic method invocation is to use wildcard
### mappings, such as <action name="*/*" method="{2}" class="actions.{1}">
struts.enable.DynamicMethodInvocation = false
 
### when set to true, Struts will act much more friendly for developers. This
### includes:
### - struts.i18n.reload = true
### - struts.configuration.xml.reload = true
### - raising various debug or ignorable problems to errors
###   For example: normally a request to foo.action?someUnknownField=true should
###                be ignored (given that any value can come from the web and it
###                should not be trusted). However, during development, it may be
###                useful to know when these errors are happening and be told of
###                them right away.
struts.devMode = false
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值