package myFirstStrust2WebProject.Model; public class myStruts2Model { private String message; public myStruts2Model( ) { super(); this.message = "this my first Struts2WebProject"; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
package myFirstStrust2WebProject.Action; import myFirstStrust2WebProject.Model.myStruts2Model; import com.opensymphony.xwork2.ActionSupport; public class myStruts2Action extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private myStruts2Model myModel; public String execute() throws Exception { myModel = new myStruts2Model() ; return "success"; } public myStruts2Model getMyModel() { return myModel; } public void setMyModel(myStruts2Model myModel) { this.myModel = myModel; } }
添加myFirstStruts2Web.jsp,内容如下:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>myFirstWebStructs2Web!</title> </head> <body> <h2><s:property value="myModel.message" /></h2> </body> </html>
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="basicstruts2" extends="struts-default"> <action name="myFirstStruts2" class="myFirstStrust2WebProject.Action.myStruts2Action" method="execute"> <result name="success">/myFirstStruts2Web.jsp</result> </action> </package> </struts>
<constant name="struts.devMode" value="true" />
struts.devMode也就是struts的开发模式,默认值为false,改为true就是以后一旦就改这个文件中的配置就不用去重启tomcat<action name="<span style="color: rgb(57, 51, 255); font-family: Monaco; font-size: 11px;">myFirstStruts2</span>">struct.xml -> 找到对应的class -> 实例化对象 -> 执行对应的execute()方法
ActionSupport源
public String execute() throws Exception {
return SUCCESS;
}
最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法。<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>myFirstStrust2WebProject</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <a href="<%=basePath%>myFirstStruts2.action">我的第一个struts2程序</a> </body> </html>
版权声明:本文为博主原创文章,未经博主允许不得转载。
mac下 Struts2 第一个程序的详细步骤(附带源码链接)
原文:http://blog.csdn.net/liuyinghui523/article/details/48046827