用户登录问题
<-------------------------login.jsp------------------------------->
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
function chek(){
test.action="chek.action";
test.submit();
}
</script>
</head>
<body>
<form id="test" name="test" action="" method="post">
<table>
<tr>
<td>用户名:<input name=name type=text /></td>
<td>密码:<input name=password type=password /></td>
<td><input name=button type=button value="确定" onclick="chek()" /></td>
</tr>
</table>
</form>
</body>
</html>
<------------------------struts-default.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>
<package name="test_2" extends="struts-default">
<action name="chek" class="action.TestAction">
<result name="success">test_2/success.jsp</result>
<result name="error">test_2/error.jsp</result>
</action>
</package>
</struts>
<----------------------------TestAction.java---------------------------->
package action;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletRequest;
import javax.swing.event.ListDataEvent;
import org.apache.struts2.ServletActionContext;
import org.omg.CORBA.Request;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionSupport;
import DBtools.DbUtil;
public class TestAction extends ActionSupport{
private String name;
private String password;
public String chek() throws IOException, ClassNotFoundException{
boolean aaa = true;
Connection conn=null;
ResultSet rs=null;
try {
conn=DbUtil.getConn();
String sql = "select * from waf name=? password=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, getName());
pstmt.setString(2, getPassword());
rs=pstmt.executeQuery();
if(rs.next()){
aaa = true;
}else {
aaa = false;
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
DbUtil.closeConnn(conn);
}catch(SQLException e){
e.printStackTrace();
}
}
if(aaa = true){
return SUCCESS;
}else {
return ERROR;}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
登录页面输入用户名和密码点击提交后就报错
2012-1-13 14:52:27 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-default.xml]
2012-1-13 14:52:27 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to locate configuration files of the name struts-plugin.xml, skipping
2012-1-13 14:52:27 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-plugin.xml]
2012-1-13 14:52:27 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts.xml]
2012-1-13 14:52:28 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2012-1-13 14:52:28 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2012-1-13 14:52:28 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/47 config=null
2012-1-13 14:52:28 org.apache.catalina.startup.Catalina start
信息: Server startup in 3159 ms
2012-1-13 14:52:42 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find action or result
There is no Action mapped for namespace / and action name chek. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
我的成功页面和失败页面都可以用下面的地址成功打开,说明我路径没有问题
http://127.0.0.1:8080/test/test_2/success.jsp
http://127.0.0.1:8080/test/test_2/error.jsp
不明白为什么老是说我的chek的action找不到。另外帮我看一下我写的chek函数有没有问题,谢谢!
[解决办法]
只是一个简单的操作而已 你在提交的时候没必要那么麻烦啊。
1、action=“” 你提交的URL (例:test!TestAction.action)
2、检查你的配置struts.xml.