Struts2配置后跳转报错解决方法

很多人刚开始学习struts2时会遇到配置完 web.xml ,struts.xml后启工程或写一个简单的页面跳转会报错情况,我把我遇到的几种情况和解决方法贴出来分享下

1.启工程时报 ClassNotFound 这样的错误

我一般出这种问题可以通过错误信息先找引用时缺失哪个文件,大多数情况可能是jar包缺失,一般struts2需要引用以下几个包:

asm-3.3.jar
asm-commons-3.3.jar
commons-fileupload-1.3.jar
commons-io-2.2.jar
commons-lang-2.4.jar
commons-logging-1.1.3.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.16.jar
xwork-core-2.3.16.jar
commons-lang3-3.1.jar

2.写好测试类跳转出错 no Action mapped

web.xml


 
   
 
   xml version=“1.0” encoding=“utf-8”?>
«/span>web-app 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” id=“WebApp_ID” version=“2.5”>
«/span>display-name>geminimanager display-name>
«/span>filter>
«/span>filter-name>struts2 filter-name>
«/span>filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter-class>
filter>
«/span>filter-mapping>
«/span>filter-name>struts2 filter-name>
«/span>url-pattern>/* url-pattern>
filter-mapping>
«/span>welcome-file-list>
«/span>welcome-file>/login/login.jsp welcome-file>
welcome-file-list>
web-app>

struts.xml


 
    
 
    xml version="1.0" encoding="UTF-8"?>
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="test" namespace="/test" extends="struts-default">
<action name="login" class="com.gemini.action.LoginAction">
<result name="success" >/login/success.jsp result>
<result name="error">/login/error.jsp result>
action>
package>
struts>
login.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>测试登录 title>
head>
<body>
<s:form action="/test/login.action" method="post" >
<s:textfield label="用户名" name="userinfo.username"> s:textfield>
<s:password label="密码" name="userinfo.userpassword"> s:password>
<s:password label="重复密码" name="userinfo.userRePassword"> s:password>
<s:textfield label="昵称" name="userinfo.usernickname"> s:textfield>
<s:submit value="accept"> s:submit>
s:form>
body>
html>
UserInfo.java
package com.gemini.pojo;
public class UserInfo {
private String username;
private String userpassword;
private String userRePassword;
private String usernickname;
public UserInfo(String username, String userpassword,
String userRePassword, String usernickname) {
this.username = username;
this.userpassword = userpassword;
this.userRePassword = userRePassword;
this.usernickname = usernickname;
}
public UserInfo() {

}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}
public String getUserRePassword() {
return userRePassword;
}
public void setUserRePassword(String userRePassword) {
this.userRePassword = userRePassword;
}
public String getUsernickname() {
return usernickname;
}
public void setUsernickname(String usernickname) {
this.usernickname = usernickname;
}
}
LoginAction.java
package com.gemini.action;
import org.apache.commons.lang.StringUtils;
import com.gemini.pojo.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private UserInfo userinfo;
@Override
public String execute() throws Exception {
if(StringUtils.equals(userinfo.getUsername(),"liuwei")){
return SUCCESS;
}else{
return ERROR;
}
}
public UserInfo getUserinfo() {
return userinfo;
}
public void setUserinfo(UserInfo userinfo) {
this.userinfo = userinfo;
}
}
上面代码是调试好后的,成功返回页面代码略去,报过 There is no Action mapped for namespace [/] and action name…错误
归结起来有这几种情况:
1.struts.xml、struts-default等自定义命名或拼错造成识别不出来;
2.struts.xml的位置不正确,或自定了struts.xml位置,默认应该在src目录下编译后在classes根目录下(检查编译路径是不是自己改了,默认是 WebRoot/WEB-INF/classes);
3.命名空间配置出了问题,比如struts.xml里的namespace配置了”/test”,则前端login.jsp的action就必须带上”/test”,可以“/test/login.action”也可以”AAA/BBB/test/login.action”,这样写都会认,可是配置了namespace后action里却不带”/test”则会报错;
4.拦截器里的”/*”被改成其它的,比如”*.action”;
3.struts Filter生效时,报Filter版本太旧
将web.xml里的Filter改成示例中的配置方式
 
4.Struts2与servlet共同部署,调用时报错
只需要在struts.xml里增加下面内容即可

 
刚学简单配置了下遇到了这几个问题,还有问题我会继续更新解决办法…

你可能感兴趣的:(java,struts2,ssh,错误分析)