zoukankan      html  css  js  c++  java
  • Struts2_2_第一Struts2应用

    web.xml文件的配置与1同样。

    1)HelloWorld类的代码:

    public class HelloWorldAction {
    	private String message;
    
    	public String getMessage() {
    		return message;
    	}
    
    	public void setMessage(String message) {
    		this.message = message;
    	}
    
    	public String execute() {
    		message = "The First Struts2 App";
    		return "success";
    	}
    }

    2)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:用于其他包继承该包
    	  	 namespace:在訪问时唯一确定一个包的路径
    	  	 extends:继承struts-default,以实现struts的核心功能
    	  	 action中的属性:
    	  	 name:在訪问时与命名空间一起唯一确定包的action
    	  	 class:action要跳转到的action类
    	  	 method:action的实现方法
    	  	 result中的属性:
    	  	 name:action类中的实现方法会依据该名字进行跳转
    	  	 index.jsp:要跳转到的显示层
    	 -->
    	<package name="packageName" namespace="/test" 
    extends="struts-default">
    		<action name="helloworld" class="itcase.HelloWorldAction" 
    method="execute">
    			<result name="success">/index.jsp</result>
    		</action> 
    	</package>
    </struts>

    3)显示层index.jsp的代码:

    <%@ page language="java" import="java.util.*" 		
    pageEncoding="UTF-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>My JSP Page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    </head>
    
    <body>
    <!-- Ognl表达式。能够显示action获取的action类的属性 -->
    	${message }
    </body>
    </html>

    配置好上述配置,又一次公布Tomcat,若没有错误。打开浏览器,输入:http://localhost:8080/Struts_1/test/helloworld

    当中:localhost:8080为本地电脑接口;

          Struts_1:整个文件所在的web项目名称;

          Test:命名空间;

          helloworld:action的名称。









    版权声明:本文博主原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    什么是SQLCLR与使用
    SQL Server中使用正则表达式
    YUV格式
    Android官方开发文档Training系列课程中文版:手势处理之ViewGroup的事件管理
    Android中利用5.0系统屏幕录制UI漏洞骗取应用录制屏幕授权
    Android解析编译之后的所有文件(so,dex,xml,arsc)格式
    PageRank 算法--从原理到实现
    机器人视觉初级系列
    深入解析 iOS 开源项目
    微信热补丁 Tinker 的实践演进之路
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4757930.html
Copyright © 2011-2022 走看看