一、使用 NetBeans 8.0.2 建立 struct2.x 框架环境
新建Java web项目,在框架选择的时候,勾选structs2。点击完成,即会生成名为 helloworld 的项目。
struct2.x 框架的工作流程:
三、分析利用structs2框架生成的 helloworld 项目
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title><s:text name="HelloWorld.message"/></title> </head> <body> <h2><s:property value="message"/></h2> <h3>Languages</h3> <ul> <li> <s:url id="url" action="HelloWorld"> <s:param name="request_locale">en</s:param> </s:url> <s:a href="%{url}">English</s:a> </li> <li> <s:url id="url" action="HelloWorld"> <s:param name="request_locale">es</s:param> </s:url> <s:a href="%{url}">Espanol</s:a> </li> </ul> </body> </html>在上面代码中,元素<s:url>的 action 属性值必须是 struct.xml 配置过的请求名,即HelloWorld。
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!--使用example.xml配置文件--> <include file="example.xml"/> <!-- Configuration for the default package. --> <package name="default" extends="struts-default"> </package> </struts>
<?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> <!--创建名为example的包,该包必须继承默认包--> <package name="example" namespace="/example" extends="struts-default"> <!--配置helloworld请求action--> <action name="HelloWorld" class="example.HelloWorld"> <result>/example/HelloWorld.jsp</result> </action> </package> </struts>元素<action>中属性name表示请求的名称(HelloWorld),class表示处理该请求的具体执行类(example.HelloWorld)。
/* * $Id: HelloWorld.template,v 1.2 2008-03-27 05:47:21 ub3rsold4t Exp $ * * 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. */ package example; import com.opensymphony.xwork2.ActionSupport; /** * <code>Set welcome message.</code> */ public class HelloWorld extends ActionSupport { public String execute() throws Exception { setMessage(getText(MESSAGE)); return SUCCESS; } /** * Provide default valuie for Message property. */ public static final String MESSAGE = "HelloWorld.message"; /** * Field for Message property. */ private String message; /** * Return Message property. * * @return Message property */ public String getMessage() { return message; } /** * Set Message property. * * @param message Text to display on HelloWorld page. */ public void setMessage(String message) { this.message = message; } }HelloWorld.java文件就是对应名为 HelloWorld 的action。上面代码,实现了相应的业务逻辑组件。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/liuruiqun/article/details/46987021