Eclipse下struts框架的简单搭建

来源:互联网 发布:有哪些域名交易网站 编辑:程序博客网 时间:2024/06/05 10:19

一、下载Eclipse并安装J2EE模式的IDE

Eclipse地址:http://www.eclipse.org/home/index.php
点击DOWNLOAD
点击DOWNLOAD

选择下载64BIT
选择下载64BIT/32BIT

点击DOWNLOAD
这里写图片描述

不需要任何其他操作,静静等待页面弹出下载框就好,如果没有弹出也可提交邮件地址
这里写图片描述

双击下载的exe文件,选择Eclispe IDE For Java EE Developers
这里写图片描述

二、下载struts压缩包

官网地址:http://struts.apache.org/
点击DOWNLOAD下载
这里写图片描述

选择Full Distribution
这里写图片描述

解压缩放入自己常用的软件盘中
这里写图片描述

三、打开Eclipse开始搭建struts简易框架

1、new->Dynamic Web Project
这里写图片描述

在创建项目的next过程最后,记得勾选Generate web.xml deployment descriptor,系统会在webContent文件夹下自动创建web.xml文件
这里写图片描述

2、将下载的struts文件夹中的apps文件夹下的案例程序解压缩。
这里写图片描述

3、将案例程序中的lib文件夹中的jar包复制到Eclipse项目的lib文件夹下面
这里写图片描述

大部分的时候,使用struts2的web应用并不需要利用到Struts2的全部特性,因此没有必要一次将Struts2解压路径下的JAR文件全部复制到WEB应用的WEB-INF/lib路径下
如果缺少相关的jar包可去struts文件夹中的lib包中获取
这里写图片描述

4、新增文件
这里写图片描述

5、HelloWorldAction.java
继承ActionSupport

package com.imooc.action;import com.opensymphony.xwork2.ActionSupport;public class HelloWorldAction extends ActionSupport {    @Override    public String execute() throws Exception {        System.out.println("执行Action");        return SUCCESS;    }}

6、struts.xml
头文件因版本不同可能不一样,在struts文件中apps案例中,找到之前解压缩的样例文件,复制该struts.xml

<?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="default" namespace="/" extends="struts-default">        <action name="helloworld" class="com.imooc.action.HelloWorldAction">            <result>/result.jsp</result>        </action>    </package></struts>

7、web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>HelloWorld</display-name> <filter>    <filter-name>strust2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping>    <filter-name>strust2</filter-name>    <url-pattern>/*</url-pattern> </filter-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

8、result.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!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>Insert title here</title></head><body>    This is result.jsp</body></html>

9、执行程序
这里写图片描述

10、地址中输入http://localhost:8888/HelloWorld/helloworld.action
8888是Tomcat的端口号,这个在装Tomcat的时候设置的
这里写图片描述

四、常见错误及解决

1、struts框架中德jar包应该是统一的,各个jar包尽量不能分散获得,即它们的版本号应该相同。不推荐下载最新的struts,应该尽量去找稳定版的下载

2、程序中经常会报错,明明程序没有错,步骤流程也是按照书上或者视频上讲的去做的,但是就是不对。
Eclipse中Project->clean 清除缓存
其中有的教程没有明确说明清楚缓存的意思,大部分人都是在Tomcat下clean,没有效果。