Eclipse搭建struts开发环境

Eclipse搭建struts开发环境

(Eclipse版本:Neon.2 Release (4.6.2)Build id: 20161208-0600
struts2版本:2.3.30
Tomcat版本:apache-tomcat-9.0.0.M20
操作系统:ArchLinux
jdk版本:1.8.0_121
)

  • 下载开发所需环境(jdk、tomcat、eclipse、 struts2)

下载jdk1.8.0_121
链接: https://pan.baidu.com/s/1nuMuaPr 密码: 35mz


下载tomcat9.0
链接: https://pan.baidu.com/s/1hrM4oAw 密码: r6sc


下载 struts2.3.30 jar包
链接: https://pan.baidu.com/s/1c2L6AlU 密码: 5bd9


(linux下配置jdk tomcat 详见我博客Linux文章)

  • 新建动态网站工程(Dynamic Web Project)

建完后的项目结构
这里写图片描述

开始把struts2的必需jar包 复制到 WebContent/WEB-INF/lib 下
(struts2.3.30必需基本包)
这里写图片描述
(复制完后)
这里写图片描述

  • 修改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>Struts2Demo1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>
  • 在src目录下新建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="hello" class="com.action.TestAction">
            <result>/success.jsp</result>
        </action>
    </package>
</struts>
  • 在WebContent目录下新建index.jsp和success.jsp
    index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:a action="hello">Hello</s:a>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="hello" /> 
</body>
</html>
  • 新建对应的Action类-TestAction
    新建包com.action

    新建类TestAction

“`
package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {
private String hello;

public String getHello() {
    return hello;
}

public void setHello(String hello) {
    this.hello = hello;
}

@Override
public String execute() throws Exception {
    // TODO Auto-generated method stub
    //return super.execute();
    hello="Hello,world";
    return SUCCESS;
}

}

`

  • 完毕
    至此项目结构如下
    这里写图片描述

    • 开始调试运行
      运行效果
      这里写图片描述
      这里写图片描述
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值