ITeye博客

struts入门案例

cakin24 · 2017-10-19 20:47 · Struts · 编程语言 · Markdown

一 创建一个Web project

 

名称为HelloWorld,创建界面如下:


 

 

二 安装struts

 

截图如下:


 


 

 

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

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

        xmlns="http://java.sun.com/xml/ns/javaee";

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";

        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd";>

  <display-name></display-name>  

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <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></web-app>

 

 

四 框架自动生成struts

 

<?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>

 

</struts>   

 

五 创建action

 

截图如下:


 


 

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文件

 

<?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="default" namespace="/" extends="struts-default">

                <action name="helloworld" class="com.cakin.action.HelloWorldAction">

                        <result>/result.jsp</result>

                </action>

        </package>

</struts>

 

七 创建result.jsp文件

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'result.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>

    This is my JSP page. <br>

  </body>

</html>

 

八 测试

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。