1、创建工程项目:
File->new->others,打开新建向导对话框,在树中找到web->Dynamic Web Project,选中,点击next按钮。在projects name中输入dodotest,点击next。保持默认设置,点击finished。这时,我们在eclipse中会看到新建的工程dodotest,创建完成。
2、修改web.xml文件:
内容如下
Xml代码
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" 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>
- dodotest</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>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>
dodotest</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、编写类代码:
3.1 HelloWorld.java
先写一个父类
Java代码
- package com.yeepal.test;
-
- public class HelloWorld {
- private String words;
-
- public String getWords() {
- return words;
- }
-
- public void setWords(String words) {
- this.words = words;
- }
- }
package com.yeepal.test;
public class HelloWorld {
private String words;
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
}
3.2 HelloAction.java
再写一个子类
Java代码
- package com.yeepal.test;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.yeepal.test.HelloWorld;
-
- public class HelloAction extends ActionSupport {
- private static final long serialVersionUID = 1L;
-
- private HelloWorld helloWorld;
-
- public HelloWorld getHelloWorld() {
- return helloWorld;
- }
-
- public void setHelloWorld(HelloWorld helloWorld) {
- this.helloWorld = helloWorld;
- }
-
- @Override
- public String execute() {
- return SUCCESS;
- }
- }
package com.yeepal.test;
import com.opensymphony.xwork2.ActionSupport;
import com.yeepal.test.HelloWorld;
public class HelloAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private HelloWorld helloWorld;
public HelloWorld getHelloWorld() {
return helloWorld;
}
public void setHelloWorld(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
@Override
public String execute() {
return SUCCESS;
}
}
注:这里分成两个类来写,是为了顺便实践一下类的继承,而不是必须的,只写一个类也是可以的。
4、加载相应的包:
写完上面的类,会发现有些错误,主要是因为我们这里使用了STRUTS及一些相应的lib文件,而这些文件我们还没添加到项目里,所以下一步我们就来添加这些lib,本次要使用到的lib文件包括4个,分别是:struts2-core-2.0.9.jar、freemarker-2.3.8.jar、 ognl-2.6.11.jar、xwork-2.0.4.jar,千万不要一下子就把所有的struts里面的lib文件全部拿过来用,反而会出错。
把这写文件放到 dodotest\WebContent\WEB-INF\lib 目录下,在eclipse中右击项目名称,选择properties,在新的属性对话框里,选择java Build Path,然后再右侧的对话框里选择Libraries标签,然后是右边的“Add External JARs…,找到刚才那4个lib文件,全选,再点“打开”,这样,这4个lib文件就被添加到了项目中,这时候你会发现刚才的那些错误消失了。
5、现在我们来写struts.xml文件:
在src目录下新建XML文件,命名为struts.xml,内容如下:
Xml代码
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <include file="struts-default.xml" />
-
- <package name="default" extends="struts-default">
- <action name="hello"
- class="com.yeepal.test.HelloAction">
- <result name="success">success.jsp</result>
- <result name="input">index.jsp</result>
- </action>
-
- </package>
- </struts>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="default" extends="struts-default">
<action name="hello"
class="com.yeepal.test.HelloAction">
<result name="success">success.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
6、编写JSP文件:
下面我们来写两个JSP文件,新建JSP文件,默认情况下新建的文件路径是WebContent下,不需要修改。
Html代码
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>你好,世界</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>
- <form action="hello.action" method="post">
- <fieldset><legend>Struts2入门实例</legend>
- <p><label>请输入你想说的话:</label>
- <input type="text" name="helloWorld.words" value="试试看!" /></p>
- <p><input type="submit" value="提交" /></p>
- </fieldset>
- </form>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>你好,世界</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
dis
分享到:
相关推荐
JAVA Eclipse 配置Struts2
最详细eclipse配置struts2最近下载了最新的struts 2.06,在使用其中附带的例子时,在配置上遇到了一些问题。 经过很多次的努力后,终于配置成功。现在把配置过程写出来供大家参考!
在Eclipse中配置Struts2项目(html)手把手教会你 如何在Eclipse中配置Struts2。 struts2 标签库介绍(html)对Struts2的标签做了详细的介绍让你轻松掌握Struts2的标签。 STRUTS2学习文档.pdf 对Struts2的一些配置...
Eclipse中配置Struts2项目指南 ,介绍Eclipse中如何配置Struts2项目
Eclipse配置一个struts2实例,struts2配置一个简单的项目
在eclipse中struts2提示设置 0积分才是分享的王道
struts2 在eclipse 配置成功
SSH(struts2,Hibernate,Spring)整合及测试亲测可用,百分之白亲测可用。
eclipse中struts文件配置工具,更快的、开发struts
Eclipse搭建SSM(Eclipse+Struts2+Spring+MyBatis)框架全过程,每一个步骤都非常详细,注解简单明了,还有每个步骤的配图,非常适合初学者!
本文首先对 Easy Struts 及其安装配置步骤进行了简介,并通过一个循序渐进的例子介绍了如何构建一个基本的 Struts 应用程序。本文还介绍了增强 Struts 应用程序的各种方法,例如连接数据库,对应用程序进行模块化,...
完整的使用MyEclipse 实现struts项目的启动工作,适合初学者
1.加载struts-2.3.16.3,把画面跑出来,不连数据库,看一下有消息验证 2.加载 hibernate-distribution-3.3.2.GA-dist.tar.gz,连数据库,看正常插入。 3.删除Hibernate的配置文件Hibernate.cfg.xml,工厂类...
eclipse配置struts2所需的jar包. 具体做法: 1.将jar包放在lib文件下 2.配置web.xml <filter> <filter-name>struts2</filter-name> <!-- 过滤器的名字 --> <filter-class>org.apache.struts2.dispatcher....
详细讲了在eclipse如何配置struts框架,算是入门级的
对于Struts2在Eclipse_3.5_javaee、Tomcat6.0、jdk1.5环境下配置与实现。Struts2具体版本为Struts2-2.2.1
一套基于增删改查的基础ssh整合,数据库需要设置正确连接信息
eclipse for javaee配置web开发环境(struts2+servlet+sping)字体.doc 让你快速学会在eclipse for javaee里开发javaweb项目