崎岖的诞生过程
说明一下,这并非是说代码难敲什么的。从昨晚敲完书本上的代码后,一直就不能正常的测试!!!觉得有点迷茫,甚至有点灰心。。。可是,可是,转念一想,这不是长姿势的好机会么?于是,慢慢找答案吧。
OK!回到正题,简单说说我遇到的几个错误,以及解决方案.
小应用简述
这是一个登陆验证的特别简单的例子,共有以下几个配置文件:
- 登陆页:index.jsp
- 欢迎页:welcome.jsp
- Action:LoginAction
这个例子就是从登陆页输入用户名和密码,然后发送至Action处理,如果用户名和密码正确,则跳转到weclome.jsp页面,否则留在index.jsp页面.
struts2 action class not found
这是我使用的搜索词,大致意思是:当表单提交的时候,访问不了Action…这可苦逼了,第一次配置Struts就遇到这事,心中难免失落……不过也不能不做啊!于是找各种答案,各种尝试,反正就是访问不了.
其实解决方案并不是很难,在提交表单的时候写成下面这个样子就行了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> </head> <body> <s:form action="action_struts/loginA.action" method="post"><!-- 写成loginA或者loginA.action都是不能的 --> <s:label value="登录信息"></s:label> <s:textfield name="username" label="用户名"></s:textfield> <s:password name="userpass" label="密码"></s:password> <s:submit value="登录"></s:submit> </s:form> </body> </html> |
Struts2 抛 java.lang.NoSuchMethodException
这个方法指的是找不到指定的方法,所以抛出这个异常。
抛出这个异常的原因是,下面这段代码写成这样了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 2013.8.16 --> <package name="first" extends="struts-default" namespace="/N12"><!-- 定义一个package --> <!-- 对action返回结果的配置 --> <action name="loginA" class="action_struts.LoginAction" method="{1}" ><!--这一句多余: method="{1}" --> <result name="success">/welcome.jsp</result> <result name="login">/index.jsp</result> </action> </package> <!-- END 2013.8.16 --> </struts> |
重复的action
这个问题我还没有找到解决办法,具体错误如下:每成功的登录一次,URL中就会重复一次[Action类所在包的包名,例如包名是:actions],那么每登录成功一次,包名就会多一个.
形如:
1 | localhost:8080/TEST/A10/actions/[actions...+]/LoginAction.action |
完整的代码
测试可用.
系统环境如下:
- tomcat 7.0.35
- Eclipse 4.2
- struts 2
- jdk 1.7
index.jsp[首页][所在文件夹:WebContent]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> </head> <body> <s:form action="action_struts/loginA.action" method="post"> <s:label value="登录信息"></s:label> <s:textfield name="username" label="用户名"></s:textfield> <s:password name="userpass" label="密码"></s:password> <s:submit value="登录"></s:submit> </s:form> </body> </html> |
welcome.jsp[欢迎页][所在文件夹:WebContent]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> </head> <body> 登录成功,欢迎您,<s:property value="username" /><!-- 显示Action中的username属性 --> </body> </html> |
struts.xml[struts核心配置文件][所在文件夹:WebContent/WEB-INF/classes/]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 2013.8.16 --> <package name="first" extends="struts-default" namespace="/"><!-- 定义一个package --> <!-- 对action返回结果的配置 --> <action name="loginA" class="action_struts.LoginAction" > <result name="success">/welcome.jsp</result> <result name="login">/index.jsp</result> </action> </package> <!-- END 2013.8.16 --> </struts> |
web.xm[所在文件夹:WebContent/WEB-INF/]l:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>SSH</display-name> <welcome-file-list> <welcome-file>index.jsp</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> |
Action:LoginAction.java[Java Resources:src>[package:action_struts]]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package action_struts; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private String username ;//用户名 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } private String userpass ;//密码. public String execute(){//主方法 if("mr".equalsIgnoreCase(username)&& "123".equals(userpass)){//匹配用户名和密码 return SUCCESS;//匹配成功进入欢迎界面. } return LOGIN;//匹配失败进入登录界面 } } |
struts的jar包请放在WebContent/WEB-INF/lib/文件夹内,我是导入了所有jar包.
一切弄好之后:打开浏览器,输入[localhost:8080/[项目名]/index.jsp],就能测试了[用户名:mr,密码:123].
继续前行,COM ON!~