代码改变世界

struts2 hello world

2012-10-08 17:07  lj jiang  阅读(206)  评论(0)    收藏  举报
    网上能找到的资料很丰富,但是非常让人失望, 真不知道各位所谓的大侠是故意为难初学者还是觉得不屑于注意这些细节。但是对于真正的初学者来说,这么不负责任的行为会让人对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.exe
myeclipse-8.6.1-win32.exe
apache-tomcat-7.0.21.exe
struts-2.3.4.1-all.zip
二、过程记录
1、配置myeclipse 设置tomcat 7
2、在任意目录创建一个空的web project
3、在myeclipse 中add deployment
4、在myeclipse 中run server
5、在myeclipse 中点右键,open in browser
6、目前已能看到简单效果。
7、给web-inf/bin 添加引用:
asm-x.x.jar
asm-commons-x.x.jar
asm-tree-x.x.jar
commons-fileupload-X.X.X.jar
commons-io-X.X.X.jar
commons-lang-X.X.X.jar
commons-lang3-X.X.X.jar - as from version 2.3.3 Struts 2 bases on Commons Lang 3, but
there are still external dependencies that base on Commons Lang.
freemarker-X.X.X.jar
javassist-X.X.X.jar
ognl-X.X.X.jar
struts2-core-X.X.X.X.jar
xwork-core-X.X.X.jar
8、修改web-inf/web.xml 文件
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-cla
ss>
</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.java
package 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.java
package 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> <meta
http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Basic Struts 2
Application - Welcome</title> </head> <body> <h1>Welcome To Struts 2!</h1> <p><a
href="<s:url action='hello'/>">Hello World</a></p> </body> </html>
15、在myeclipse 中操作,redeploy application
16、Restart server
17、Open in browser 即可看到效果。

18、完美结束。