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

Eclipse搭建struts开发环境

2017-05-22 19:01 344 查看

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>
</
b0f2
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;
}


}

`

完毕

至此项目结构如下



开始调试运行

运行效果





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse struts