zoukankan      html  css  js  c++  java
  • struts2基础---->第一个Struts2程序

      学习struts2的第一个程序,这里只会涉及到简单的代码编写。有一个夜晚我烧毁了所有的记忆,从此我的梦就透明了;有一个早晨我扔掉了所有的昨天,从此我的脚步就轻盈了。

    Struts的项目

    一、引入struts2支持的jar包,在web.xml中配置struts2的拦截器。

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

    二、定义一个简单的首页index.jsp,用于跳转到struts的Action。

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h3><a href="hello.action">Hello World</a></h3>
    </body>
    </html>

    三、定义一个Action,这是struts中处理的最小单元。

    package com.huhx.struts;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HuhxAction extends ActionSupport {
        private static final long serialVersionUID = 1L;
    
        @Override
        public String execute() throws Exception {
            System.out.println("hello world.");
            return SUCCESS;
        }
    }

    四、在src下创建的struts.xml中定义acton映射

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts> 
        <package name="default" namespace="/" extends="struts-default">
            <action name="hello" class="com.huhx.struts.HuhxAction">
                <result name="success">/huhx.jsp</result>
            </action>
        </package>
    </struts>

    五、定义一个action返回的页面huhx.jsp。

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h2>Hello huhx.</h2>
    </body>
    </html>

    友情链接

  • 相关阅读:
    python学习之ajax和可视化管理工具
    操作系统-保护模式中的特权级下
    redis 分布式锁的 5个坑,真是又大又深
    数据库之数据表控制语句
    【NoSQL】Consul中服务注册的两种方式
    netstat命令使用方法以及详解
    Dockerfile与Dockerfile实战
    Spring boot+redis实现消息发布与订阅
    怎么寻回位置不可用移动硬盘的数据
    python字符前面u,r,f等含义
  • 原文地址:https://www.cnblogs.com/huhx/p/baseStruts1.html
Copyright © 2011-2022 走看看