---
title: "struts入门案例"
author: "cakin24"
date: "2017-10-19T20:47:07Z"
tags: []
categories: ["编程语言"]
slug: "struts"
draft: false
lastmod: "2017-10-19T20:49:09Z"
id: 2396989
url: "https://cakin24.iteye.com/blog/2396989"
---

一 创建一个Web project

 

名称为HelloWorld，创建界面如下：

![](http://dl2.iteye.com/upload/attachment/0127/4564/e315c444-79ec-3790-ab1d-bcb70e6a4ccc.png)  
 

 

二 安装struts

 

截图如下：

![](http://dl2.iteye.com/upload/attachment/0127/4566/f57a12f3-24de-359b-ac35-60deacb59e68.png)  
 

![](http://dl2.iteye.com/upload/attachment/0127/4568/f7c71686-dc4d-32b1-8c43-9d2db22693d3.png)  
 

 

三 生成项目后，框架自动生成过滤器

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app version="3.0"

        xmlns="[http://java.sun.com/xml/ns/javaee"](http://java.sun.com/xml/ns/javaee%22);

        xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22);

        xsi:schemaLocation="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)

[http://java.sun.com/xml/ns/javaee/web-app\_3\_0.xsd"](http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd%22);&gt;

  &lt;display-name&gt;&lt;/display-name&gt;  

  &lt;welcome-file-list&gt;

    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

  &lt;/welcome-file-list&gt;

  &lt;filter&gt;

        &lt;filter-name&gt;struts2&lt;/filter-name&gt;

        &lt;filter-class&gt;

                org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

        &lt;/filter-class&gt;

  &lt;/filter&gt;

  &lt;filter-mapping&gt;

        &lt;filter-name&gt;struts2&lt;/filter-name&gt;

        &lt;url-pattern&gt;\*.action&lt;/url-pattern&gt;

  &lt;/filter-mapping&gt;&lt;/web-app&gt;

 

 

四 框架自动生成struts

 

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "[http://struts.apache.org/dtds/struts-2.1.dtd"](http://struts.apache.org/dtds/struts-2.1.dtd%22);&gt;

&lt;struts&gt;

 

&lt;/struts&gt;   

 

**五 创建action**

 

截图如下：

![](http://dl2.iteye.com/upload/attachment/0127/4570/bc9309ee-6732-36fd-8ec0-a17d4e50a4e3.png)  
 

![](http://dl2.iteye.com/upload/attachment/0127/4572/8a846bd9-42ca-3001-a508-9ca90d2bf518.png)  
 

package com.cakin.action;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class HelloWorldAction extends ActionSupport {

        @Override

        public String execute() throws Exception {

                System.out.println("执行Action");

                return SUCCESS;

        }

}

 

**六 配置struts.xml文件**

 

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "[http://struts.apache.org/dtds/struts-2.1.dtd"](http://struts.apache.org/dtds/struts-2.1.dtd%22);&gt;

&lt;struts&gt;

        &lt;package name="default" namespace="/" extends="struts-default"&gt;

                &lt;action name="helloworld" class="com.cakin.action.HelloWorldAction"&gt;

                        &lt;result&gt;/result.jsp&lt;/result&gt;

                &lt;/action&gt;

        &lt;/package&gt;

&lt;/struts&gt;

 

七 创建result.jsp文件

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'result.jsp' starting page&lt;/title&gt;

   

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

 

  &lt;/head&gt;

 

  &lt;body&gt;

    This is my JSP page. &lt;br&gt;

  &lt;/body&gt;

&lt;/html&gt;

 

八 测试

![](http://dl2.iteye.com/upload/attachment/0127/4574/c7a591bc-5a6d-327c-b87d-50a04ec4ff90.png)