<?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这个优秀又优雅的MVC框架。