HelloWorld之Struts2

本文通过一个简单的Struts2 HelloWorld实例,介绍了如何配置Struts2框架并实现数据展示。包括web.xml、struts.xml的设置及NewsAction、NewsService的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



开始深入学习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框架。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值