美文网首页
创建Struts项目

创建Struts项目

作者: 小山雀 | 来源:发表于2017-10-27 17:41 被阅读0次

目的

说实话,写这段文字的目的并不是要和谁分享什么,而是作为一个初学者,对知识的记录和熟悉。

我是做android的,却对后台情有独钟,因为我一直觉得,有前端,有后台才是一个完整的系统。通过写笔记来记录成长。

开始创建

搭建环境

这方面的文章很多,就简单提一下:JDK、Tomcat、Eclipse,有这三个就OK。
除了环境之外Struts也是必不可少,下载备用(我下载的是完整版,也就是struts-x.x.x-all,也可以下载struts-x.x.x-min-lib.zip,因为这个压缩包里的jar包刚好是创建struts项目所有必须的基本库)。

创建动态Web项目

启动Eclipse,File->New->Dynamic Web Project,输入项目名HelloWorld,按如图所示进行配置

image.png

点击finish完成项目创建。

导入Struts库

将之前下载的Struts解压到任意目录,得到如图所示的目录结构:

image.png

如果你下载的是struts-x.x.x-min-lib.zip版,那么将得到:

image.png

这样一个目录。这个目录下刚好有这些jar包:

image.png

如果你下载的是struts-x.x.x-all包,那么你也可以在lib目录下找到相应的jar文件。

将这些jar包全部拷贝到WebContent/WEB-INF/lib下:

image.png

下面这步不要忘了:选中所有这些jar包,右键->Build Path -> Add to Build Path.

编写一个Action类

在Java Resources/src目录下创建包com.xxx.action,创建HelloWorldAction类

package com.xxx.action;

public class HelloWorldAction {
    private String name;
    
    public String execute() throws Exception{
        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

创建两个JSP页面

在WebContent目录下创建index.jsp页面,用于向HelloWorldAction 提交数据:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>

    <form action="hello">
        <label>Please input name</label><br/>
        <input type="text" name="name">
        <input type="submit">
    </form>
</body>
</html>

在WebContent目录下创建HelloWorld.jsp页面,用于显示提交后的结果:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
Hello World! Hello <s:property value="name"/>
</body>
</html>

配置项目

配置web.xml:
在WebContent/WEB-INF/下创建web.xml文件,用于配置Strtuts:

<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" metadata-complete="true" version="3.1">
  <display-name>Welcome to Tomcat</display-name>
  <description>  
     Welcome to Tomcat  
  </description>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

配置struts.xml:
在WebContent/WEB-INF/下面创建classes文件夹,并且在classes文件夹下创建struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">
     
      <action name="hello" 
            class="com.xxx.action.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

如果你的项目和我的有所区别的,注意修改struts.xml中相应的配置信息,配置完成后的项目结构:

image.png

配置完成后运行项目,浏览器访问http://localhost:8080/HelloWorld/
如果你看到:

image.png

恭喜你,项目创建成功!如果你看到StatusLogger Log4j2 could not find a logging implementation错误,请关闭tomcat,clean项目,重新运行。Over。

源码地址:https://github.com/xudefei/HelloWorld

相关文章

网友评论

      本文标题:创建Struts项目

      本文链接:https://www.haomeiwen.com/subject/qjorpxtx.html