struts2-byblank案例练习(一)
前言:本文为一个最简单的struts2案例,意在熟悉struts2主干线流程,后续练习会逐步拓展案例功能
一,创建项目
1 新建动态web工程,目录如下:
2 配置web.xml,找到struts2-core-2.3.34.jar中,StrutsPrepareAndExecuteFilter.class,拷贝全路径,配置成过滤器。
<?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" id="WebApp_ID" version="3.0"> <display-name>struts2-byblankdisplay-name> <welcome-file-list> <welcome-file>index.htmlwelcome-file> welcome-file-list> <filter> <filter-name>struts2filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class> filter> <filter-mapping> <filter-name>struts2filter-name> <url-pattern>/*url-pattern> filter-mapping> web-app>
3 新建index.html
index 欢迎进入首页,2秒后自动进入HelloWord界面!
4 配置struts.xml,位于工程src下。解压struts2-core-2.3.34.jar,找到struts2-default.xml,拷贝头引用,创建struts.xml,配置一个package,继承struts-default。
<?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">
<package name="default" namespace="/" extends="struts-default">
HelloWorld.jsp
package>
5 新建 HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>HelloWord hello,world! i am struts2
二,运行结果
三,过程分析
1,访问http://localhost:8080/struts2-byblank/,web.xml配置了
2,index.html
3,web.xml配置了StrutsPrepareAndExecuteFilter过滤器,所以action请求被struts2核心包中的struts-default.xml的配置处理。
而自定义的struts.xml继承struts-default.xml, 定义了名为HelloWorld的action,返回结果为HelloWorld.jsp,所以页面跳转到http://localhost:8080/struts2-byblank/HelloWorld.jsp
参考资料:官方struts-2.3.34-all.zip文件中,struts2-blank案例