• HelloWorld之Struts2




    开始深入学习Struts2之前,还是老规矩,先来实践一个Struts2的HelloWorld实例。
    亲自动手实践后,再开始深入分析Struts2的执行流程才会有更深的认识。

    这依然是很简单的一个实例,由下面几个文件组成。

    web.xml:设置Struts2前端过滤器,Struts2的应用都要设置。
    struts.xml:设置NewsAction的路径、结果页面等信息。
    NewsAction:从NewsService得到数据,并保存到newsList变量中。
    NewService:返回几条测试数据。
    news.jsp:最终呈现结果的JSP,只是简单地使用struts2标签显示文本。

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!--
     Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at
    
          http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    -->
    
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    	version="2.4">
    
    	<display-name>Struts Web App</display-name>
    
    	<!-- Struts mappings -->
    	
    	<filter>
    		<filter-name>struts2</filter-name>
    		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    	</filter>
    	
    	<filter-mapping>
    		<filter-name>struts2</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	
    	<!-- Struts mappings END -->
    
    </web-app>
    <?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="newspkg" extends='struts-default'>
    
        	<action name="news" class="com.cdai.web.struts.NewsAction">
        	
                <result>/news.jsp</result>
                
        	</action>
        	
    	</package>
    
    </struts>
    package com.cdai.web.struts;
    
    import java.util.List;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    @SuppressWarnings("serial")
    public class NewsAction extends ActionSupport {
    
    	private NewsService newsService;
    	
    	private List<NewsDto> newsList;
    	
    	public NewsAction() {
    		newsService = new NewsService();
    	}
    	
    	@Override
    	public String execute() {
    		
    		System.out.println("In NewsAction execute()");
    		
    		newsList = newsService.getAllNews();
    		
    		return SUCCESS;
    	}
    
    	public List<NewsDto> getNewsList() {
    		return newsList;
    	}
    	
    }
    package com.cdai.web.struts;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class NewsService {
    
    	public List<NewsDto> getAllNews() {
    		List<NewsDto> newsList = new ArrayList<NewsDto>();
    		newsList.add(new NewsDto("news1", "sport news"));
    		newsList.add(new NewsDto("news2", "social news"));
    		newsList.add(new NewsDto("news3", "ecnomic news"));
    		return newsList;
    	}
    	
    }
    <%@page 
    	import="com.cdai.web.struts.*" 
    	contentType="text/html;charset=utf-8" 
    %>
    <%@taglib prefix="s" uri="/struts-tags"%>
    
    <html>
    
    	<head>
    		<title>Programmer in SY</title>	
    	</head>
    
    	<body>
    	
    		<s:iterator value="newsList">
    			<li>
    				<s:property value="title"/>				
    			</li>
    			<p>
    				<s:property value="content"/>
    			</p>
    		</s:iterator>
    	
    	</body>
    
    </html>

    实现了这个Struts2的HelloWorld实例程序后,可能我们会有些疑问:

    (1)最明显的就是Struts1的前端Servlet怎么变成Filter了?
    (2)咱们熟悉的ActionForm类哪去了?
    (3)news.jsp是怎样从NewsAction上拿到newsList的值的?

    不要着急,下面让我们一起来深入学习Struts2这个优秀又优雅的MVC框架。

  • 相关阅读:
    Python笔记 #17# Pandas: Merge
    MVC相关资料收集
    Python笔记 #16# Pandas: Operations
    Least slack time scheduling
    Python笔记 #15# Pandas: Missing Data
    Python笔记 #14# Pandas: Selection
    C++中const引用的是对象的时候只能调用该对象的f()const方法
    模板与泛型编程
    c++中的单例模式
    C/C++异常处理机制
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157813.html
Copyright © 2020-2023  润新知