日期:2014-05-19  浏览次数:20698 次

struts2的转发页面错误(404)
测试struts2中的Action是如何通过ActionContext访问Servlet API的,实例是模仿登录业务逻辑,分别把登录用户名保存在application和session中,然后从jsp页面中读取信息

可是我页面跳转的时候报404找不到页面的错误,仔细检查了struts.xml,好像也没有什么错误啊!

登录业务逻辑:LoginAction.java
package com;

import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport{
  private String name;
  private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
   
  @SuppressWarnings("unchecked")
public String checkLogin(){
  ActionContext ac = ActionContext.getContext();
  @SuppressWarnings({ "unused", "rawtypes" })
Map app = ac.getApplication();
  ac.getSession().put("login", this.name);
  ac.getApplication().put("login", this.name);
  return SUCCESS;
  }
}

web.xml页面
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

struts.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  
  
<struts>
  <include file="struts-default"/>
  <package name="default" extends="struts_default" namespace="/login">
  <action name="loginAction" class="com.LoginAction" method="checkLogin">
  <result>/index.jsp</result>
  </action>
  </package>
</struts>

login.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
  pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
  <head>
  <title>session or application</title>
  </head>
  <body style="background-image:url('images/bg-0414.gif')">
  <center>
  <br><h2>用户登录</h2>
  <s:form action="loginAction" method="post" namespace="/login">
  <s:textfield name="name" label="登录名称" size="15"/>
  <s:password name="pw