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

Struts第一个例子(HelloWorld!)

2007-11-05 00:08 393 查看
Strut学习第一个例子: HelloWorld ! (还是老样子

)

配置: Eclipse3.1.2+Struts-2.0.11+Tomcat5.0 (暂时不用Hibernate和Spring)

步骤:
1。在官网上下载struts-2.0.11-all.zip,解压后里面“几堆文件”,在apps文件夹里面有个叫struts2-blank-2.0.11.war的文件(其实也是一个包),打开它,将WEB-INF/lib下的那五个jar文件(commons-logging-1.0.4.jar;freemarker-2.3.8.jar;ognl-2.6.11.jar;struts2-core-2.0.11.jar;xwork-2.0.4.jar)复制出来,粘贴到Eclipse中新建Dynamic Web Project项目下的WEB-INF/lib里(为什么要选择struts2-blank-2.0.11.war而不是其它几个war文件,因为小



2。修改web.xml配置文件为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Hello World</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

3。在src目录下建个struts.xml和一个HelloWorld.java:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts><!-- Configuration for the default package. -->
<package name="default" extends="struts-default">

<action name="HelloWorld" class="HelloWorld">
<result name="success">/helloworld.jsp</result>
</action>

</package>
</struts>

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
private String message;
public String execute() throws Exception {
return this.SUCCESS;
}
public void setMessage(String message){
this.message = message;
}
public String getMessage() {
return message;
}
}

4。在WebContent下建两个JSP文件,分别为:index.jsp和helloworld.jsp
index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>首页</title>
</head>
<body>
<h2><s:form action="HelloWorld">
<s:textfield name="message" label="你的姓名"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form></h2>
</body>
</html>
helloworld.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hello World</title>
</head>
<body>
你好:<s:property value="message" />!
</body>
</html>

5。大功告成,启动“Tom那只猫”(Tomcat),郁闷了!出现两个个严重错误:
严重: Error filterStart
2007-11-4 23:13:21 org.apache.catalina.core.StandardContext start
严重: Context startup failed due to previous errors
2007-11-4 23:13:24 org.apache.catalina.core.StandardHost getDeployer
以前没遇见过,咋办,好像跟filter有点关,于是去掉web.xml的filter标签,没问题了,但是struts也玩完儿了,应为根本映射不了struts!经过千辛万苦,终于知道了一点点,就是下载个xalan-2.7.0.jar文件(download:http://www.java2s.com/Code/Jar/AXIS2/Downloadxalan270jar.htm
将其放在Tomcat5.0\common\endorsed下,重启“Tom那只猫”,OK! 万里长征开始了!

本文出自 “JohnWey程序员日记” 博客,请务必保留此出处http://opengreat.blog.51cto.com/264115/49029
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: