做了一个structs2的demo,在structs.xml配置文件的package中,没有namespace=“/”就报404错,请问这是为什么?
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>interceptWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>login.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
structs.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- <package name="defaultAction" namespace="/" extends="struts-default"> -->
<package name="authority" extends="struts-default"> <!-- 如果没有namespace="/"就会出现404错误,请问这是为什么? -->
<!-- 定义一个拦截器 -->
<interceptors>
<interceptor name="authority" class="com.test.action.LoginInterceptor">
</interceptor>
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<!-- 定义全局Result -->
<global-results>
<result name="login">/test.jsp</result>
</global-results>
<action name="login" class="com.test.action.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="loginform" class="com.test.action.LoginFormAction">
<result name="success">/login.jsp</result>
</action>
<action name="show" class="com.test.action.ShowAction">
<result name="success">/show.jsp</result>
<interceptor-ref name="mydefault"/>
</action>
</package>
</struts>
LoginAction.java
package com.test.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
public String execute(){
System.out.println(username);
System.out.println(password);
if (isInvalid(getUsername()))
return INPUT;
if (isInvalid(getPassword()))
return INPUT;
if(this.getUsername().equals("admin")&& this.getPassword().equals("123")){
ActionContext.getContext().getSession().put(Constants.USER_SESSION,getUsername());
ActionContext.getContext().getSession().put(Constants.PASS,getPassword());
return "success";
}
return "error";
}
}
感谢各位大神指点!