mac下 Struts2 第一个程序的详细步骤(附带源码链接)
来源:互联网 发布:知乎 赛博朋克 编辑:程序博客网 时间:2024/06/06 12:29
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,运行查看结果。
工程源码链接
1 0
- mac下 Struts2 第一个程序的详细步骤(附带源码链接)
- mac下Myeclipse2015编写的第一个hibernate程序myHibernateUser及源码
- 第一个IOS程序的开发过程以及详细步骤介绍(一)
- 第一个IOS程序的开发过程以及详细步骤介绍(二)
- struts2的第一个程序
- Struts2的第一个程序
- 第一个struts2的程序
- MAC环境下Android Studio的第一个JNI程序
- Mac下的第一个C++程序—HelloWorld
- struts2 的第一个程序hello struts2
- 开发第一个C程序的步骤
- 第一个struts2程序
- 第一个struts2程序
- struts2第一个程序
- struts2 第一个程序
- 第一个struts2程序
- Struts2第一个程序
- 第一个struts2程序
- 获取IP地址
- 面向对象的思想
- Flex 布局基础语法篇
- App 控件定位讲解 (hierarchyviewer 篇)
- Git中的AutoCRLF与SafeCRLF换行符问题
- mac下 Struts2 第一个程序的详细步骤(附带源码链接)
- 004Median of Two Sorted Arrays
- 最新基于adt-bundle-windows-x86的android开发环境搭建
- Object类
- 利用jquery实现在html的表格中使用上下左右键切换单元格,同时只限制单元格输入数字
- iOS字符串
- 每天5道面试题(五)java基础
- java里类方法和实例方法区别
- Java读取配置文件的几种方法以及路径问题