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

只愿长相守 提交于 2020-02-29 08:43:51

<strong>很多人刚开始学习struts2时会遇到配置完 web.xml ,struts.xml后启工程或写一个简单的页面跳转会报错情况,我把我遇到的几种情况和解决方法贴出来分享下</strong> <p><strong>1.启工程时报 ClassNotFound 这样的错误</strong></p> <p>我一般出这种问题可以通过错误信息先找引用时缺失哪个文件,大多数情况可能是jar包缺失,一般struts2需要引用以下几个包:</p> <p>asm-3.3.jar<br>asm-commons-3.3.jar<br>commons-fileupload-1.3.jar<br>commons-io-2.2.jar<br>commons-lang-2.4.jar<br>commons-logging-1.1.3.jar<br>freemarker-2.3.19.jar<br>javassist-3.11.0.GA.jar<br>ognl-3.0.6.jar<br>struts2-core-2.3.16.jar<br>xwork-core-2.3.16.jar<br>commons-lang3-3.1.jar</p> <p><strong>2.写好测试类跳转出错 no Action mapped </strong></p> <p><font color="#0000ff">web.xml</font></p> <div><pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #0000ff"><?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">="1.0"</span> <span style="color: #ff0000">encoding</span><span style="color: #0000ff">="utf-8"</span>?<span style="color: #0000ff">></span><br><span style="color: #0000ff"><</span><span style="color: #800000">web-app</span> <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://java.sun.com/xml/ns/javaee"</span> <span style="color: #ff0000">xmlns:xsi</span><span style="color: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</span> <span style="color: #ff0000">xsi:schemaLocation</span><span style="color: #0000ff">="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="WebApp_ID"</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">="2.5"</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">display-name</span><span style="color: #0000ff">></span>geminimanager<span style="color: #0000ff"></</span><span style="color: #800000">display-name</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">filter</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">filter-name</span><span style="color: #0000ff">></span>struts2<span style="color: #0000ff"></</span><span style="color: #800000">filter-name</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">filter-class</span><span style="color: #0000ff">></span>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<span style="color: #0000ff"></</span><span style="color: #800000">filter-class</span><span style="color: #0000ff">></span><br> <span style="color: #0000ff"></</span><span style="color: #800000">filter</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">filter-mapping</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">filter-name</span><span style="color: #0000ff">></span>struts2<span style="color: #0000ff"></</span><span style="color: #800000">filter-name</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">url-pattern</span><span style="color: #0000ff">></span>/*<span style="color: #0000ff"></</span><span style="color: #800000">url-pattern</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"></</span><span style="color: #800000">filter-mapping</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file-list</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>/login/login.jsp<span style="color: #0000ff"></</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span> <br> <span style="color: #0000ff"></</span><span style="color: #800000">welcome-file-list</span><span style="color: #0000ff">></span> <br><span style="color: #0000ff"></</span><span style="color: #800000">web-app</span><span style="color: #0000ff">></span><br></pre></div>

<div><font color="#0000ff" size="2">struts.xml</font></div> <div> <div id="codeSnippetWrapper"><pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #0000ff">&lt;?</span><span style="color: #800000">xml</span> <span style="color: #ff0000">version</span><span style="color: #0000ff">="1.0"</span> <span style="color: #ff0000">encoding</span><span style="color: #0000ff">="UTF-8"</span>?<span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;!</span><span style="color: #800000">DOCTYPE</span> <span style="color: #ff0000">struts</span> <span style="color: #ff0000">PUBLIC</span><br> <span style="color: #0000ff">"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"</span><br> <span style="color: #0000ff">"http://struts.apache.org/dtds/struts-2.3.dtd"</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;</span><span style="color: #800000">struts</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">package</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="test"</span> <span style="color: #ff0000">namespace</span><span style="color: #0000ff">="/test"</span> <span style="color: #ff0000">extends</span><span style="color: #0000ff">="struts-default"</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">action</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="login"</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">="com.gemini.action.LoginAction"</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">result</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="success"</span> <span style="color: #0000ff">&gt;</span>/login/success.jsp<span style="color: #0000ff">&lt;/</span><span style="color: #800000">result</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">result</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="error"</span><span style="color: #0000ff">&gt;</span>/login/error.jsp<span style="color: #0000ff">&lt;/</span><span style="color: #800000">result</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">action</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">package</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">struts</span><span style="color: #0000ff">&gt;</span></pre><span style="color: #0000ff"><font size="2">login.jsp</font></span></div> <div> <div id="codeSnippetWrapper"> <div id="codeSnippetWrapper"> <div id="codeSnippetWrapper"> <div id="codeSnippetWrapper"><pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="background-color: #ffff00">&lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt;</span><br><span style="background-color: #ffff00">&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;</span><br><span style="color: #0000ff">&lt;!</span><span style="color: #800000">DOCTYPE</span> <span style="color: #ff0000">HTML</span> <span style="color: #ff0000">PUBLIC</span> <span style="color: #0000ff">"-//W3C//DTD HTML 4.01 Transitional//EN"</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;</span><span style="color: #800000">meta</span> <span style="color: #ff0000">http-equiv</span><span style="color: #0000ff">="Content-Type"</span> <span style="color: #ff0000">content</span><span style="color: #0000ff">="text/html; charset=utf-8"</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;</span><span style="color: #800000">title</span><span style="color: #0000ff">&gt;</span>测试登录<span style="color: #0000ff">&lt;/</span><span style="color: #800000">title</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;/</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">s:form</span> <span style="color: #ff0000">action</span><span style="color: #0000ff">="/test/login.action"</span> <span style="color: #ff0000">method</span><span style="color: #0000ff">="post"</span> <span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">s:textfield</span> <span style="color: #ff0000">label</span><span style="color: #0000ff">="用户名"</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="userinfo.username"</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">s:textfield</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">s:password</span> <span style="color: #ff0000">label</span><span style="color: #0000ff">="密码"</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="userinfo.userpassword"</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">s:password</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">s:password</span> <span style="color: #ff0000">label</span><span style="color: #0000ff">="重复密码"</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="userinfo.userRePassword"</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">s:password</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">s:textfield</span> <span style="color: #ff0000">label</span><span style="color: #0000ff">="昵称"</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="userinfo.usernickname"</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">s:textfield</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;</span><span style="color: #800000">s:submit</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="accept"</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">s:submit</span><span style="color: #0000ff">&gt;</span><br> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">s:form</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;/</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span><br><span style="color: #0000ff">&lt;/</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span></pre><font color="#0000ff" size="2">UserInfo.java</font><br> <div id="codeSnippetWrapper"><pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #0000ff">package</span> com.gemini.pojo;<br><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> UserInfo {<br> <span style="color: #0000ff">private</span> String username;<br> <span style="color: #0000ff">private</span> String userpassword;<br> <span style="color: #0000ff">private</span> String userRePassword;<br> <span style="color: #0000ff">private</span> String usernickname;<br> <span style="color: #0000ff">public</span> UserInfo(String username, String userpassword,<br> String userRePassword, String usernickname) {<br> <span style="color: #0000ff">this</span>.username = username;<br> <span style="color: #0000ff">this</span>.userpassword = userpassword;<br> <span style="color: #0000ff">this</span>.userRePassword = userRePassword;<br> <span style="color: #0000ff">this</span>.usernickname = usernickname;<br> }<br> <span style="color: #0000ff">public</span> UserInfo() {<br> <br> }<br> <span style="color: #0000ff">public</span> String getUsername() {<br> <span style="color: #0000ff">return</span> username;<br> }<br> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setUsername(String username) {<br> <span style="color: #0000ff">this</span>.username = username;<br> }<br> <span style="color: #0000ff">public</span> String getUserpassword() {<br> <span style="color: #0000ff">return</span> userpassword;<br> }<br> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setUserpassword(String userpassword) {<br> <span style="color: #0000ff">this</span>.userpassword = userpassword;<br> }<br> <span style="color: #0000ff">public</span> String getUserRePassword() {<br> <span style="color: #0000ff">return</span> userRePassword;<br> }<br> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setUserRePassword(String userRePassword) {<br> <span style="color: #0000ff">this</span>.userRePassword = userRePassword;<br> }<br> <span style="color: #0000ff">public</span> String getUsernickname() {<br> <span style="color: #0000ff">return</span> usernickname;<br> }<br> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setUsernickname(String usernickname) {<br> <span style="color: #0000ff">this</span>.usernickname = usernickname;<br> }<br>}<br></pre><font color="#0000ff" size="2">LoginAction.java</font><br> <div id="codeSnippetWrapper"><pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4"><span style="color: #0000ff">package</span> com.gemini.action;<br><span style="color: #0000ff">import</span> org.apache.commons.lang.StringUtils;<br><span style="color: #0000ff">import</span> com.gemini.pojo.UserInfo;<br><span style="color: #0000ff">import</span> com.opensymphony.xwork2.ActionSupport;<br><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> LoginAction <span style="color: #0000ff">extends</span> ActionSupport {<br> <span style="color: #0000ff">private</span> UserInfo userinfo;<br> @Override<br> <span style="color: #0000ff">public</span> String execute() <span style="color: #0000ff">throws</span> Exception {<br> <span style="color: #0000ff">if</span>(StringUtils.equals(userinfo.getUsername(),<span style="color: #006080">"liuwei"</span>)){<br> <span style="color: #0000ff">return</span> SUCCESS;<br> }<span style="color: #0000ff">else</span>{<br> <span style="color: #0000ff">return</span> ERROR;<br> }<br> }<br> <span style="color: #0000ff">public</span> UserInfo getUserinfo() {<br> <span style="color: #0000ff">return</span> userinfo;<br> }<br> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setUserinfo(UserInfo userinfo) {<br> <span style="color: #0000ff">this</span>.userinfo = userinfo;<br> }<br>}<br></pre>上面代码是调试好后的,成功返回页面代码略去,报过<font color="#ff0000"><strong>There is no Action mapped for namespace [/] and action name</strong></font>…错误</div></div></div></div></div> <div>归结起来有这几种情况:</div> <div>1.struts.xml、struts-default等自定义命名或拼错造成识别不出来;</div> <div>2.struts.xml的位置不正确,或自定了struts.xml位置,默认应该在src目录下编译后在classes根目录下(检查编译路径是不是自己改了,默认是 WebRoot/WEB-INF/classes);</div> <div>3.命名空间配置出了问题,比如struts.xml里的namespace配置了”/test”,则前端login.jsp的action就必须带上”/test”,可以“/test/login.action”也可以”AAA/BBB/test/login.action”,这样写都会认,可是配置了namespace后action里却不带”/test”则会报错;</div> <div>4.拦截器里的”/*”被改成其它的,比如”*.action”;</div> <div><strong>3.struts Filter生效时,报Filter版本太旧</strong></div> <div>将web.xml里的Filter改成示例中的配置方式</div> <div>&nbsp;</div> <div><strong>4.Struts2与servlet共同部署,调用时报错</strong></div> <div>只需要在struts.xml里增加下面内容即可</div> <p>&lt;constant name="struts.action.extension" value="action"&gt;&lt;/constant&gt; <div>&nbsp;</div> <div>刚学简单配置了下遇到了这几个问题,还有问题我会继续更新解决办法…</div></div></div></div>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!