未调用操作

时间:2014-08-10 10:20:41

标签: jsp web-applications struts2 xml-configuration

单击“提交”按钮时,不会调用操作reg1

我的简单struts应用程序如下:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee z     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Struts2</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.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

reg.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd">
 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="reg1" >
    Username: <input type="text" name="username"> Password: <input
        type="text" name="password"> Mobile: <input type="text"
        name="mobile"> <input type="submit" >
</form>
</body>
</html>

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="default" extends="struts-default" namespace="/">
    <action name="reg1.action" class="bean.regbean">
        <result name="success">/login.jsp</result>
    </action>
</package>
</struts>

1 个答案:

答案 0 :(得分:2)

未调用该操作,因为它未正确映射到表单操作属性中的url。使用此操作配置映射操作名称,该名称在没有.action后缀的情况下使用。

<action name="reg1" class="bean.regbean">
    <result name="success">/login.jsp</result>
</action>

另请注意,最新的Struts2版本中不推荐使用FilterDispatcher。因此,您必须相应地升级和修改web.xml。在JSP中,您可以使用struts标记将字段绑定到bean属性。