struts

警告: No configuration found for the specified action: '/book/login' in namespace: '/book'. Form action defaulting to 'action' attribute's literal value.

 

今天测试maven打包,遇到这个警告,在晚上查了些资料解决了这个问题,记录下来,恭喜给大家:

出问题前:

源码struts.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>  
   
    <package name="book" namespace="/book" extends="struts-default">  
        <action name="login" class="struts2.LoginAction" method="login">  
            <result name="success">../views/login.jsp</result>  
        </action>  
    </package>  
   
</struts> 

 

login.jsp显示信息如下

<%@ page contentType="text/html; charset=UTF-8"%>  
<%@ taglib prefix ="s" uri="/struts-tags"%>
<html>  
<head></head>  
<body>  
    <h1>Struts 2 Hello World Example</h1>  
   
    <s:form action="/book/login">  
        <s:textfield name="username" label="Username" value="%{username}"/> 
        <s:property value="username"/> 
        <s:password name="password" label="Password" />  
    	<s:submit />  
    </s:form>  
   
</body>  
</html> 

 最后查了相关资料发现,是struts标签使用出的问题,struts标签会自动在后面加上.action,是因为struts标签中没有namespace属性,所以struts会直接去根目录下查找'/book/login',所以查找不到本来该login.action,将jsp改为如下源码就正确了:

<%@ page contentType="text/html; charset=UTF-8"%>  
<%@ taglib prefix ="s" uri="/struts-tags"%>
<html>  
<head></head>  
<body>  
    <h1>Struts 2 Hello World Example</h1>  
   
    <s:form action="login" namespace="/book">  
        <s:textfield name="username" label="Username" value="%{username}"/> 
        <s:property value="username"/> 
        <s:password name="password" label="Password" />  
    	<s:submit />  
    </s:form>  
   
</body>  
</html> 

 这样struts从book包下查找login.action就没问题了。

你可能感兴趣的:(struts)