您的位置:首页 > 编程语言 > Java开发

mac下 Struts2 第一个程序的详细步骤(附带源码链接)

2015-08-28 15:29 609 查看

mac下 Struts2 第一个程序的详细步骤

1.新建web工程

(1) 打开MyEclipse 2015,选择File->New->Web Porject 菜单,新建一个web project。



(2)配置Project信息
填写工程名字myFirstStrust2WebProject,选择默认项目存放路径,选在javaEE 6版本。



选择下一步,再下一步,选择默认创建默认首页index.jsp和web.xml(web.xml文件作用)文件,这两个文件也可以后来添加,然后选择完成。





2.下载struts2框架

struts2下载链接 ,选中all链接,下载struts2文件包。解压该文件,得到struts-2.3.24文件夹,下有apps文件夹(存放Struts2的一些示例程序),docs文件夹(存放Struts2的参考文档),lib文件夹(存放Struts2的jar包),src文件夹(存放Struts2的源代码)。




解压该文件,得到struts-2.3.24文件夹,下有apps文件夹(存放Struts2的一些示例程序),docs文件夹(存放Struts2的参考文档),lib文件夹(存放Struts2的jar包),src文件夹(存放Struts2的源代码)。



lib文件夹存放Struts2的所有jar包,我们只需要挑出来必须要用的11个jar包就可以了。







3.在工程中引入struts2框架

把上一步选出的11个jar包放到工程的lib文件下。



在MyEclipse Explorer中,选中项目,右键,Build Path->Configure Build Path













4.配置第一个Struts2框架web程序





1添加model类

添加myStruts2Model类,代码如下:


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;
	}
	
}


2 添加action类

添加myStruts2Action类,代码如下:

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;
	}
	
}


3在WebRoot下添加jsp文件

添加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>


4.添加struts.xml

在解压好的文件夹里找到struts-2.3.24/apps/struts2-blank/WEB-INF/src/java/struts.xml,复制到项目struts2-blank的src下,并做修改,Myeclipse会自动将struts.xml布署到WEB-INF\classes下的。

<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()方法

执行过程:

读到xml -> action是class -> 找到class对象(每次访问必须new一个对象) ->

当不配置class的时候,默认的class是ActionSupport。

ActionSupport源

public String execute() throws Exception {


return SUCCESS;

}
最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法。

5添加struts2 Filter

找到项目的“web.xml”文件,然后在web.xml文件中加入Struts2
Filter的配置信息:
<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>



通过配置这个Filter,启动Struts框架,StrutsPrepareAndEXecuteFilter()方法中将会读取类路径下的默认文件struts.xml完成初始化操作。Struts将struts.xml文件内容,以javabean的形式存放在内存中,从而不必重复读取struts.xml文件。

6. 重写index.jsp

重写index.jsp,内容如下:
<%@ 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>


5运行程序

部署到tomcat,运行查看结果。
工程源码链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: