标签:des style blog http color java os io
第一个Struts程序 ---Hello World
第一步: 下载安装struts
1. 下载链接: http://archive.apache.org/dist/struts/binaries/
2. 从网站上下载的Struts2包含了二三十个库文件,但大多数是可选的,这里只需要struts2-blank-*.war里WEB-INF/lib的库文件。
在Eclipse里 Window->Preferences->Java->Build Path->User Libraries 添加struts的user libraries(新建然后引入包)
3. 新建web工程,右键工程选择Build Path->Add Libraries,将之前建立的struts的user libraries引入。
4. 在工程的WEB-INF/web.xml中配置struts的拦截器
##注意填入的filter-class可能因为版本不同而有所不同
<!-- Struts2 配置 --> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5. 配置struts.xml,配置要调用的action,如果直接建立在src目录下,部署时会自动发布到WEB-INF/classses目录
<?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> <package name="struts" extends="struts-default"> <action name="HelloWorld" class="tutorial.HelloWorld"> <result>/HelloWorld.jsp</result> </action> </package> </struts>
package类似于java的包机制,action对应于表单所填的的action=”FromAction”,这里的action运行后会转到HelloWorld.jsp
6. 编写action类,即tutorial.HelloWorld
package tutorial; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport { public final static String MESSAGE = "Struts2 is up and running ..."; private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String execute() throws Exception{ setMessage(MESSAGE); return SUCCESS; } }
7. 编写jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% 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 Hello World Struts</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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h2><s:property value="message" /></h2> <br> </body> </html>
8. 部署运行,打开浏览器输入: http://localhost:8080/HelloWorldStruts/HelloWorld (tomcat默认8080端口)
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/dao0/p/3907398.html