报错信息
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 后来缓存清楚后 代码跑起来了
但是还是不知道为啥报错