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

    友情链接

  • 相关阅读:
    Linux下监视GPU、CPU的使用情况
    2014多校第一场 E 题 || HDU 4865 Peter's Hobby (DP)
    2014多校第一场 I 题 || HDU 4869 Turn the pokers(费马小定理+快速幂模)
    2014多校第一场D题 || HDU 4864 Task (贪心)
    2014多校第一场J题 || HDU 4870 Rating(DP || 高斯消元)
    2014多校第一场A题 || HDU 4861 Couple doubi
    POJ 2948 Martian Mining(DP)
    POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)
    POJ 3280 Cheapest Palindrome(DP)
    POJ 4044 Score Sequence
  • 原文地址:https://www.cnblogs.com/huhx/p/baseStruts1.html
Copyright © 2020-2023  润新知