当前位置: 代码迷 >> Java Web开发 >> Struts 零配置 后 仍然访问根路径,何解
  详细解决方案

Struts 零配置 后 仍然访问根路径,何解

热度:912   发布时间:2016-04-13 22:12:46.0
Struts 零配置 后 依然访问根路径,何解?
初学struts2, 联系零配置,代码如下

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>      
    <constant name="struts.convention.result.path" value="/WEB-INF/pages" /> 
    <constant name="struts.convention.package.locators" value="web,action" />
</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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsTest</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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
      <param-name>actionPackages</param-name>
      <param-value>action</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


divLogin.jsp
[
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<%@taglib uri="/struts-dojo-tags" prefix="sx"%>
<html>
<head>
<s:head/>
<s:head theme="xhtml"/>
<sx:head parseContent="true"/>
</head>
<body>
<div class="div" id="loginDiv">
<dir id="errorDiv"></dir>
<s:form name="divLoginForm" id="divLoginForm">
<s:label label="请输入用户名密码"></s:label>
<s:textfield name="username" label="帐号" value="helloween"></s:textfield>
<s:password name="password" label="密码" value="1234" title="1234"></s:password>
<s:url action="divLogin" id="divLoginUrl" method="login"></s:url>
<sx:submit value="登陆" formId="divLoginForm" href="%{divLoginUrl}" targets="loginSuccessDiv"
executeScripts="true"></sx:submit>
</s:form>
</div>

<div id="loginSuccessDiv" class="div" style="display: none;"></div>

</body>
</html>


divLoginScript.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<%@taglib uri="/struts-dojo-tags" prefix="sx" %>
<html>
<head>

</head>
<body>
<s:bean id="date" name="java.util.Date"></s:bean>
<s:if test="%{#request.status == 'success'}">
欢迎您,<s:property value="#session.username"/>. 登陆时间:<s:date name="%{#date}"/>.
<s:url action="divLogin" id="divLogoutUrl" method="logout"></s:url>
<sx:a href="%{#divLogoutUrl}" executeScripts="true">注销</sx:a>

<script>
document.getElementById('errorDiv').innerHTML = '';
document.getElementById('loginDiv').style.display = 'none';
document.getElementById('loginSuccessDiv').style.display = '';
</script>
<s:elseif test="%{#request.status == 'failed'}">
<script>
document.getElementById('errorDiv').innerHTML = '登陆失败,错误的用户名密码';
</script>
</s:elseif>
<s:else>
document.getElementById('loginDiv').style.display = '';
document.getElementById('loginSuccessDiv').innerHTML = '';
document.getElementById('loginSuccessDiv').style.display = 'none';
</s:else>

</s:if>


</body>
</html>



Action 类
package action;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;
@Results(value={@Result(name="input",location="divLogin.jsp"),@Result(name="script",location="divLoginScript.jsp")})
@Action(value="/divLogin")
@SuppressWarnings("serial")
public class DivLoginAction extends ActionSupport{

private String username;
private String password;

@Override
public String execute() throws Exception {
return "input";
}

public String login() {
if("helloween".equalsIgnoreCase(username) && "1234".equalsIgnoreCase(password)){
ServletActionContext.getRequest().getSession(true).setAttribute("username", username);
ServletActionContext.getRequest().setAttribute("status", "success");
}else{
ServletActionContext.getRequest().setAttribute("status", "failed");
}

return "script";

}

public String logout(){
ServletActionContext.getRequest().getSession(true).removeAttribute("username");
ServletActionContext.getRequest().setAttribute("status", "logout");
return "script";
}

}


访问 http://localhost:8080/StrutsTest2/divLogin 可以正常映射到 divLogin.jsp.
但是,点击提交按钮,报错
Could not find action or result: /StrutsTest2/divLogin!login.action?struts.enableJSONValidation=true
There is no Action mapped for namespace [/] and action name [divLogin!login] associated with context path [/StrutsTest2]. - [unknown location]

这是什么原因呢?
------解决思路----------------------
 http://localhost:8080/StrutsTest2/divLogin

/StrutsTest2/divLogin!login.action?struts.enableJSONValidation=true

一个加action 一个不加?
  相关解决方案