zoukankan      html  css  js  c++  java
  • Struts2入门(1)-第一个Struts2程序

    目录结构

    C:WorkSpacejavaStrutsTestsrcmain
    C:WorkSpacejavaStrutsTestsrc	est
    C:WorkSpacejavaStrutsTestsrcmainjava
    C:WorkSpacejavaStrutsTestsrcmain
    esources
    C:WorkSpacejavaStrutsTestsrcmainwebapp
    C:WorkSpacejavaStrutsTestsrcmainjavacom
    C:WorkSpacejavaStrutsTestsrcmainjavacomchzhao
    C:WorkSpacejavaStrutsTestsrcmainjavacomchzhaostrutstest
    C:WorkSpacejavaStrutsTestsrcmainjavacomchzhaostrutstestHelloAction.java
    C:WorkSpacejavaStrutsTestsrcmain
    esourcesstruts.xml
    C:WorkSpacejavaStrutsTestsrcmainwebapphello.jsp
    C:WorkSpacejavaStrutsTestsrcmainwebappWEB-INF
    C:WorkSpacejavaStrutsTestsrcmainwebappWEB-INFweb.xml
    C:WorkSpacejavaStrutsTestsrc	estjava
    C:WorkSpacejavaStrutsTestsrc	est
    esources
    

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.zhao</groupId>
    	<artifactId>StrutsTest</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>StrutsTest</name>
    	<description>StrutsTest</description>
    
    	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>3.8.1</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.struts</groupId>
    			<artifactId>struts2-core</artifactId>
    			<version>2.3.16.1</version>
    		</dependency>
    		<dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    			<version>1.2.14</version>
    		</dependency>
    	</dependencies>
    </project>
    

    struts.xml

    <?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="test" namespace="/" extends="struts-default">
    		<action name="hello" class="com.chzhao.strutstest.HelloAction" method="hello">
    			<result name="success">/hello.jsp</result>
    		</action>
    	</package>
    </struts>
    

    HelloAction.java

    package com.chzhao.strutstest;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloAction extends ActionSupport {
        
    	private static final long serialVersionUID = 1L;
    	private String str;
        public String hello() {
           this.str = "zhaokeke!!!";
           return "success";
        }
        public String getStr() {
           return str;
        }
        public void setStr(String str) {
           this.str = str;
        }
    }
    

    hello.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!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>Test</title>
    </head>
    <body>
    	<h1>
    		<s:property value="str" />
    	</h1>
    </body>
    </html>
    

    运行程序

    修改Run Configurations,把context改为/,然后run as jetty
    打开浏览器,输入http://localhost:8080/hello.action即可。

  • 相关阅读:
    带结点与不带结点用头插法和尾插法创建单链表
    高中数学教资面试记录
    学习爬虫——test1——模拟浏览器去访问网站
    解决centos虚拟机中使用virt-manager创建虚拟机遇到的镜像导入问题
    pycharm更改背景颜色
    控制函数调用的流程
    简单的获取list的下标
    种一棵树的时间,不是以前,也不是未来的某一天,而是现在。
    将八位教师随机分配到三个办公室——python
    使用tomcat部署idea项目方法(1)
  • 原文地址:https://www.cnblogs.com/wardensky/p/5777560.html
Copyright © 2011-2022 走看看