1. 程式人生 > 其它 >struts2內建form標籤配合模型驅動配置action再IDEA中標籤name屬性標紅

struts2內建form標籤配合模型驅動配置action再IDEA中標籤name屬性標紅

技術標籤:bug報錯struts2

報錯資訊

There is no Action mapped for namespace [/] and action name [register] associated with context path [/struts2_03].

表單:

<%--
    @program: struts2_02
    @description
    @author: LIANG
    @create: 2021-01-22 11:19
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--匯入struts標籤庫--%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<
html
>
<head> <title>login</title> </head> <body> <s:fielderror></s:fielderror> <s:form action="register" namespace="/user"> <s:textfield name="username" label="使用者名稱"
>
</s:textfield><br> <s:password name="password" label="密碼"></s:password><br> <s:textfield name="birthday" label="生日"></s:textfield><br> <%-- 使用ognl表示式 --%> <
s:checkboxlist
list="#{'':'吃飯','':'喝水','':'睡覺'}" label="愛好" name="hobby">
</s:checkboxlist> <s:radio list="#{'true':'已婚','false':'未婚'}" label="是否已婚" name="married"></s:radio> <s:submit value="註冊" ></s:submit> </s:form> </body> </html>

action:

package org.ccit.com.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.ccit.com.domain.User;
import org.ccit.com.service.IUserService;
import org.ccit.com.service.impl.UserServiceImpl;


/**
 * @program: struts2_02
 * @description
 * @author: LIANG
 * @create: 2021-01-22 11:22
 **/
public class UserAction extends ActionSupport implements ModelDriven<User> {

    //宣告變數時new
    private User user=new User();
    @Override
    public User getModel() {
        return user;
    }
    /**
     * @Description: 註冊功能實現
     * @param
     * @return: java.lang.String
     * @Author: LIANG
     * @Date: 2021/1/22 16:25
     */
    public String register(){
        //呼叫業務方法
        IUserService iUserService = new UserServiceImpl();
        System.out.println(user.toString());
        iUserService.register(user);
        return NONE;
    }
}

在這裡插入圖片描述
IDEA 提示:
在這裡插入圖片描述

有毒…
剛開始 無法執行 以為無法使用模型驅動封裝
但是 過了會時間 可以運行了 雖然標紅但是執行也不報錯(估計剛開始是快取問題)
但還是不知道為什麼標紅 新增action後 name屬性就會標紅

There is no Action mapped for namespace [/] and action name [register] associated with context path [/struts2_03].
猛然發現 報錯更action寫法有聯絡
action有兩種寫法
一種寫/user/register.action (再action屬性中連同名稱空間寫全)// 這種在2.5版本中好像不適用了
一種是action屬性中只寫action名 namesapce屬性填寫名稱空間

報錯是因為 我起初沒有寫namespace屬性 acthon中也只寫了register 後來快取清楚後 程式碼跑起來了
但是還是不知道為啥報錯