最基本的struts搭建问题

今天试着搭建struts框架,但是却总是无法访问,求兄弟姐妹们帮忙...

配置文件和struts包都是按教科书上配置的,struts的版本是struts-2.1.8.1,复制到项目上的struts类为:

commons-fileupload-1.2.1.jar,commons-logging-1.0.4.jar,freemarker-2.3.15.jar,

ognl-2.7.3.jar,struts2-core-2.1.8.1.jar,xwork-core-2.1.6.jar
具体情况是:

项目名:struts_login

 

action:

com.loginAction
package com;

import com.opensymphony.xwork2.ActionSupport;

public class loginAction extends ActionSupport 
{
    private String loginname;
    private String pwd;
    
    public String getLoginname() {
        return loginname;
    }
    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    
    public String checkLogin()
    {
        if(this.loginname.equals("admin")&&this.pwd.equals("admin"))
            return SUCCESS;
        else
            return LOGIN;
    }
}

 

jsp页面:

index.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%>">
    
    <title>登录成功</title>
    <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">
    -->
  </head>
  
  <body>
    <center>
        <h2>登录成功,这里是系统后台</h2>
    </center>
  </body>
</html>
 
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>用户登录</title>
    
    <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">
    -->
  </head>

  <body>
    <center>
        <br/>
        <s:form action="checkLogin" namespace="/login">
        <s:textfield name="loginname" label="登录名称"></s:textfield>
        <s:textfield name="pwd" label="登录密码"></s:textfield>
        <s:submit value="登录"></s:submit>
        </s:form>
    </center>
    
  </body>
</html>
 

 

配置文件:

web.xml
<?xml version="1.0" encoding="UTF-8" ?> 
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
  <filter-name>struts2</filter-name> 
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
  </filter>
<filter-mapping>
  <filter-name>struts2</filter-name> 
  <url-pattern>/*</url-pattern> 
  </filter-mapping>
 </web-app>

 

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>
    <include file="struts-default.xml"></include>
    <package name="Struts 2.0_login" extends="struts-default" namespace="/login">
            <action name="checkLogin" class="com.loginAction" method="checkLogin"> 
                <result name="success">/index.jsp</result>
                <result name="login">/login.jsp</result>
            </action>
    </package>
</struts>

 

 

当我试着用:http://localhost:8080/struts_login访问时,出来的总是经典的404...

大神们,求解决啊...

这个问题我也遇到过,,你在MyEclipse里面改了名字了,,,但是tomcat里面还是原来的项目名,这样是访问不到的。你在MyEclipse里右击那个项目名选择properties选项,在里面找到web一项,在web context-root里把你那个项目名给为现在的那个,,应该就行了。。。。

什么错误你也没说啊 我感觉是action="checkLogin.action"

你别用struts2标签 直接用jsp语言试试

要不你把项目给我 我帮你改完在给你

qq 71214645

呵呵,路径错误,自己改下就好了



改成试下呢

你点击自己要访问的那个页面或者action右键属性看下它的路径,再看下你的400错误的路径就好了,相信自己,路径问题细心点就好了。其他的不会错的。

仔细检查下配置文件的namespace和页面的namespace是否匹配
改成试下

There is no Action mapped for namespace / and action name checkLogin. - [unknown location]
没有找到namespace为/的命名空间



你的表单没有指定method,提交方式,你加上看看呢

action="checkLogin.action"

这些其实都不是问题,这是每一个编程人员都会遇到的,而且错误的原因都各种各样,自己好好找找就OK