• 周四. 2月 29th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

idea maven新建struts2项目

admin

11月 28, 2021

环境:

IDEA

java1.8

struts2-core  2.5.18

一路下一步,名字自己随便填,

项目建好后修改pom.xml文件,加入struts2-core

添加tomcat:

 +号添加web

添加tomcat

在resources下新建struts.xml

如果出现错误,Exception starting filter struts2
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

 手动删除再添加这个,然后修改tomcat的设置。

 运行举例:

public class HelloWorldAction {
    private String name;

    public String execute() throws Exception {
        if (getName().equals("") || getName() == null)
            return "error";
        return "success";
    }

    public String getName() {
        return name;
    }

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

HelloWorldAction.java

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />

    <package name="default" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index" >
            <result name="success">/index.jsp</result>
        </action>
        <action name="hello" class="HelloWorldAction" method="execute">
            <result name="success">/HelloWorld.jsp</result>
            <result name="error">/Error.jsp</result>
        </action>
    </package>
</struts>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <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>
    <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>
</web-app>

web.xml

<%--
  Created by IntelliJ IDEA.
  User: Flyuz
  Date: 2018/12/19
  Time: 20:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
<h1>Hello World Struts2</h1>
<form action="hello">
    <label for="name">Please enter your name</label><br/>
    <input type="text" name="name"/>
    <input type="submit" value="Enter"/>
</form>
</body>
</html>

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Flyuz
  Date: 2018/12/19
  Time: 20:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Hello World</title>
</head>
<body>
Hello World, Welcome! <s:property value="name"/>
</body>
</html>

HelloWorld.jsp

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注