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

一个关于SSH2的问题~!急!
由于项目需求我需要使用SSH2框架。其中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>

<!-- devMode模式是开发模式,默认开启了一些提示功能,方便开发排错 -->
<constant name="struts.devMode" value="true" />

<!-- 交给spring管理 -->
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.objectFactory.spring.autoWire" value="type" />

<!-- 映射JSP目录 -->
<constant name="struts.convention.result.path" value="/WEB-INF/pages/" />

<!-- 包路径包含test的将被视为Action存在的路径来进行搜索 -->
<constant name="struts.convention.package.locators" value="web,action" />

<!-- URL分割规则:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割 -->
<constant name="struts.convention.action.name.separator" value="-" />

<!-- 配置默认的Action -->
  <package name="default" namespace="/" extends="struts-default"/>
</struts>


我的Action:

package com.z.test.web;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;


import com.opensymphony.xwork2.ActionSupport;

@Namespace("/")
@ParentPackage("default")
@Action
public class HelloAction extends ActionSupport{

private static final long serialVersionUID = 1L;

public String method1(){
String a = "method1";
System.out.println(a);
return SUCCESS;
}

public String method2(){
String b = "method2";
System.out.println(b);
return SUCCESS;
}
}


然后我通过URL访问:http://localhost/SSH2andJPA/hello!method1.action
可是报错:
HTTP Status 404 - There is no Action mapped for namespace / and action name hello.
There is no Action mapped for namespace / and action name hello. - [unknown location]

说是没有映射"/"这个命名空间和Action"hello"。。。不知道哪里出了问题。

------解决方案--------------------
这些配置是没错了,难道是少包了?
------解决方案--------------------
若楼主换成xml配置action的话 能否访问到 ?若能 那肯定是注解配置有问题 或者哪里配置
不正确
------解决方案--------------------
你的@Action沒給名字,
默認好想是类名,应该http://localhost/SSH2andJPA/HelloAction!method1.action
要不h小写试试,
要这样的话
http://localhost/SSH2andJPA/hello!method1.action
@Action(vlaue="hello")

------解决方案--------------------
先试试把XML中的中文注释去掉。有些版本的容器不支持中文注释。
------解决方案--------------------
你直接使用struts零配置多好啊!struts.xml代码:
<struts>
<!-- 默认包 --> <constant name="struts.convention.default.parent.package" value="auth-default"/>

<!-- 如果不用注解默认包就这样 --> <constant name="struts.convention.default.parent.package" value="convention-default"/>
<!-- 基于什么包 --> <constant name="struts.convention.package.locators.basePackage"value="这里写你自己acion路径比如:com.action"/>
<!--确定搜索包的路径。只要是结尾为action的包都要搜索。basePackage按照默认不用配置,如果配置,只会找以此配置开头的包。locators及locators.basePackage都是一组以逗号分割的字符串。 --> <constant name="struts.convention.package.locators" value="action"/>