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

struts2 hello world

2012-10-08 17:07 148 查看
网上能找到的资料很丰富,但是非常让人失望, 真不知道各位所谓的大侠是故意为难初学者还是觉得不屑于注意这些细节。但是对于真正的初学者来说,这么不负责任的行为会让人对java望而生畏的。

先说 “精通struts2.pdf” 第29页,name.jsp文件的第13行居然写错,把helloworld.cation写成helloword.action ,害得我找了半天才找到问题所在。 并且这一个段落中明显少文件,hello.jsp就没有提到。

再说 “Struts2入门教程.pdf”。第6页struts配置文件,里面的action类跟本段代码中实际的命名空间根本不一样,所以根本运行不起来,哎,太让人失望了。

不说这些丧气话了。说说我是怎么做的吧。(注意:因为java里面不用的技术对不同jar文件的版本依赖比较强,我会列出我的版本,其他的版本我不敢保证完全没问题。)

另外,建议直接看自带的document ,这个比较靠谱。目录就在struts-2.3.4.1-all的解压目录里面。

我的第一个Struts2项目HelloWord一、我使用的软件版本jdk-6u5-windows-i586-p.exemyeclipse-8.6.1-win32.exeapache-tomcat-7.0.21.exestruts-2.3.4.1-all.zip二、过程记录1、配置myeclipse 设置tomcat 72、在任意目录创建一个空的web project3、在myeclipse 中add deployment4、在myeclipse 中run server5、在myeclipse 中点右键,open in browser6、目前已能看到简单效果。7、给web-inf/bin 添加引用:asm-x.x.jarasm-commons-x.x.jarasm-tree-x.x.jarcommons-fileupload-X.X.X.jarcommons-io-X.X.X.jarcommons-lang-X.X.X.jarcommons-lang3-X.X.X.jar - as from version 2.3.3 Struts 2 bases on Commons Lang 3, butthere are still external dependencies that base on Commons Lang.freemarker-X.X.X.jarjavassist-X.X.X.jarognl-X.X.X.jarstruts2-core-X.X.X.X.jarxwork-core-X.X.X.jar8、修改web-inf/web.xml 文件<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>/*</url-pattern></filter-mapping>9、在src 目录里面添加文件struts.xml<?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><constant name="struts.devMode" value="true" /><package name="basicstruts2" extends="struts-default"><action name="index"><result>/index.jsp</result></action></package></struts>此时亦能运行,不过已经是添加过struts 功能的简单程序了。下面我们添加struts 支持的一个简单功能。10、创建model:MessageStore.javapackage org.apache.struts.helloworld.model;public class MessageStore {private String message;public MessageStore() {setMessage("Hello Struts User");}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}}11、创建action:HelloWordAction.javapackage org.apache.struts.helloworld.action;import org.apache.struts.helloworld.model.MessageStore;import com.opensymphony.xwork2.ActionSupport;public class HelloWorldAction extends ActionSupport {private static final long serialVersionUID = 1L;private MessageStore messageStore;public String execute() throws Exception {messageStore = new MessageStore() ;return SUCCESS;}public MessageStore getMessageStore() {return messageStore;}public void setMessageStore(MessageStore messageStore) {this.messageStore = messageStore;}}12、创建view:HelloWord.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>Hello World!</title></head><body><h2><s:property value="messageStore.message" /></h2></body></html>13、修改struts.xml 增加映射<?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><constant name="struts.devMode" value="true" /><package name="basicstruts2" extends="struts-default"><action name="index"><result>/index.jsp</result></action><action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction"method="execute"><result name="success">/HelloWorld.jsp</result></action></package></struts>14、给index.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> <metahttp-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Basic Struts 2Application - Welcome</title> </head> <body> <h1>Welcome To Struts 2!</h1> <p><ahref="<s:url action='hello'/>">Hello World</a></p> </body> </html>15、在myeclipse 中操作,redeploy application16、Restart server17、Open in browser 即可看到效果。18、完美结束。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: