idea创建一个struts2的hello案例

本文介绍了在IDEA中创建Struts2 HelloWorld案例时遇到的问题及解决方案,包括jar包版本选择、过滤器版本对应、Action映射错误以及<s:property>标签无法获取值的故障排查。
摘要由CSDN通过智能技术生成

目录

Demo

问题


因为公司有个别项目是用SSH框架做的,所以就准备学习以下struts2框架,在网上搜了一下struts2的教程(w3的),刚看到创建demo的时候,就出现了一大堆问题,后来一看教程就错了,折磨了我一下午,以下把问题和demo罗列出来:

Demo

HelloWorldAction
package com.struts.controller;

public class HelloWorldAction {
    private String name ;
    private String age;

    public String execute() throws Exception {
        return "success";
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

struts2.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.struts.controller.HelloWorldAction"
                method="execute">
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

web.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">


    <display-name>Struts 2</display-name>

    <welcome-file-list>
        <welcome-file>index.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>

HelloWorld.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
name, <s:property value="getName()"/><br>
age, <s:property value="age"/>
</body>
</html>

 index.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>
  <title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action="hello">
  <input type="text" name="name"/>
  <input type="text" name="age"/>
  <input type="submit" value="Say Hello"/>
</form>
</body>
</html>

问题

1,jar包资源的问题(注意jar包版本,我这里用的是2.2.3)

struts2的jar包依赖兼容性真是太差了,多了也不行,少了也不行

 2,过滤器的问题

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

目前是发现了三个版本(具体版本号记不住了,仅供参考)

2.2以下的是

org.apache.struts2.dispatcher.FilterDispatcher

2.2到2.3的是

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

2.3以上的是

        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>

3,程序报错(There is no Action mapped for namespace / and action name)

大概意思就是找不到对应的Action,网上查到的说法有很多,大概就是说struts2.xml没加载到,idea我是这么解决的

 4,<s:property value="age"> 取不到值的问题

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值