做了一个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";
}
}
感谢各位大神指点!
<?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";
}
}
感谢各位大神指点!
...全文
相关推荐
Structs的基本配置
第一步:配置ActionServlet 第二步:初始化参数对Servlet的运行环境进行初始化配置。
HTTP Status 404 - There is no Action
HTTP Status 404 - There is no Action mapped for namespace and action name BackMemberGroupAudit..doc
初学者入门:Structs中基本配置入门
很不错关于JAVA的Struts中基本配置入门的文档!推荐大家下载哦!
struts2 配置文件
Struts应用采用两个基于XML配置文件来配置,分别是web.xml和struts-cofig.xml文件.web.xml文件 是配置所有web应用而struts-config.xml文件是struts专用配置文件,事实上也是可以根据需要给这个配置文件 起其他名称.
Struts2学习笔记2011(详细图解)
structs2很详细的学习笔记,structs2的建造,工作原理,例子,逐步讲解


Web 开发
