当前位置: 代码迷 >> Web前端 >> struts2警告: Form action defaulting to 'action' attribute's literal value
  详细解决方案

struts2警告: Form action defaulting to 'action' attribute's literal value

热度:711   发布时间:2012-07-24 17:47:58.0
struts2警告: Form action defaulting to 'action' attribute's literal value.

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<%

??? String path = request.getContextPath();

??? String basePath = request.getScheme() + "://"

?????????? + request.getServerName() + ":" + request.getServerPort()

?????????? + path + "/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

??? <head>

?????? <base href="<%=basePath%>">

?

?????? <title>My JSP 'index.jsp' starting page</title>

?????? <meta http-equiv="pragma" content="no-cache">

?????? <meta http-equiv="cache-control" content="no-cache">

?????? <meta http-equiv="expires" content="0">

?????? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

?????? <meta http-equiv="description" content="This is my page">

?????? <!--

??? <link rel="stylesheet" type="text/css" href="styles.css">

??? -->

??? </head>

?

??? <body>

?????? <s:form action="/www/user.action" method="post">

?????????? <s:textfield label="username" name="user.username" /><s:property value="errors.username"/>

?????????? <s:password label="password" name="user.password" /><s:property value="errors.password"/>

?????????? <s:submit value="login"></s:submit>

?????? </s:form>

??? </body>

</html>

?

struts配置

<?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.enable.DynamicMethodInvocation" value="true" />

??? <constant name="struts.devMode" value="true" />

??? <constant name="struts.locale" value="zh_CN" />

??? <constant name="struts.i18n.encoding" value="gb2312" />

??? <constant name="struts.custom.i18n.resources" value="UserAction"></constant>

??? <!--

?????? <constant name="struts.custom.i18n.resources"

?????? value="example/struts2-blank" />

??? -->

?

??? ?<package name="default" extends="struts-default" namespace="/www">

?

?????? <default-action-ref name="index" />

?

?????? <global-results>

?????????? <result name="error">/error.jsp</result>

?????? </global-results>

?

?????? <global-exception-mappings>

?????????? <exception-mapping exception="java.lang.Exception"

????????????? result="error" />

?????? </global-exception-mappings>

?

?????? <action name="user" class="action.UserAction">

?????????? <result>/select.jsp</result>

?????????? <result name="input">/login.jsp</result>

?????? </action>

??? </package>

?

??? <!-- Add packages here -->

?

</struts>

?

导致发出警告

警告: No configuration found for the specified action: 'user.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.(在命名空间没有发现指定的action配置:"user.action”。默认为'action'属性的常量值

当然如果直接写<form>不会出现这个警告

报错原因1:struts2-core-2.2.3.jar\org\apache\struts2\default.properties文件下默认配置有: struts.action.extension=action,,

Struts2会自动为你完成这个工作,并且手工添加是不行的,就不必多此一举了。但是在其他的场合,比如使用超级链结,则可以加上这个.action。

报错原因2:是没有正确使用tag的原因,这种情况下,正确的写法应该是:<s:form action="user" method="post" namespace="/www">
原因分析:

因为开始使用的struts2标签(form)并未指定namespace属性。所以struts2会默认从根命名空间"/" 搜索action '/www/user.action',如搜索不到则进入默认命名空间''搜索action串,在默认命名空间中是肯定找不到自己定义的action的,所以,struts2抛出一个警告信息。

所以正常配置为

<action name="user" class="action.UserAction" namespace="/www">
?????? <result name="success">/select.jsp</result>
?????? <result name="input">/login.jsp</result>
??</action>

如果没有命名空间,显示写出namespace,则:

?<action name="user" class="action.UserAction" namespace="">
?????? <result name="success">/select.jsp</result>
?????? <result name="input">/login.jsp</result>
??</action>

  相关解决方案