chenxiaopiaoliang 2009-08-05 14:11
浏览 384
已采纳

我的struts怎么也运行不成功

我的开发环境是struts2.11,Myeclipse6.5,问题出现是:找不到action(就是那个404),还有,没有部署struts时,只写了一个jsp(webroot目录下),访问时突然是http://localhost/struts/WebRoot/login.jsp
我以前写的都是用http://localhost/struts/login.jsp(其中struts是项目名称)访问的。
下面是我的代码:WebRoot目录下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%>">
   
    <title>My JSP 'login.jsp' starting page</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>
   <form action="login.action" method="post">
   username:<input type="text" name="username"><br>
   password:<input type="password" name="password"><br>
   <input type="submit" value="Submit">
   </form>
  </body>
</html>


WebRoot目录下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%>">
   
    <title>My JSP 'result.jsp' starting page</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>
  username:${requestScope.username }<br>
  password:${requestScope.password }
  </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>

<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>

</struts>


LoginAction.java:

package com.test.action;

public class LoginAction {

private String username;
private String password;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String execute() throws Exception {
return "success";
}
}
五个jar包全部加入了,因为是struts2.11,所以加5个jar包就行了。

以上的错误在其他项目中也是一样,愁死我了,又不知道什么原因。各位师傅,谢谢了。


问题补充

chenwenhu 写道
我的开发环境是struts2.11,Myeclipse6.5,问题出现是:找不到action(就是那个404),还有,没有部署struts时,只写了一个jsp(webroot目录下),访问时突然是http://localhost/struts/WebRoot/login.jsp
我以前写的都是用http://localhost/struts/login.jsp(其中struts是项目名称)访问的。
下面是我的代码:WebRoot目录下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%>">
   
    <title>My JSP 'login.jsp' starting page</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>
   <form action="login.action" method="post">
   username:<input type="text" name="username"><br>
   password:<input type="password" name="password"><br>
   <input type="submit" value="Submit">
   </form>
  </body>
</html>


WebRoot目录下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%>">
   
    <title>My JSP 'result.jsp' starting page</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>
  username:${requestScope.username }<br>
  password:${requestScope.password }
  </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>

<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>

</struts>


LoginAction.java:

package com.test.action;

public class LoginAction {

private String username;
private String password;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String execute() throws Exception {
return "success";
}
}
五个jar包全部加入了,因为是struts2.11,所以加5个jar包就行了。

以上的错误在其他项目中也是一样,愁死我了,又不知道什么原因。各位师傅,谢谢了。


补充:我在MyEclipse6.5中写struts.xml是没有提示的。不知道为什么
  • 写回答

2条回答 默认 最新

  • iteye_15225 2009-08-06 09:29
    关注

    1:在struts.xml中修改:

    明确指出这个Package的namespace。

    2:在login.jsp中修改:

    这样应该就可以解决了找不到action的问题了。

    至于访问路径变成http://localhost/struts/WebRoot/login.jsp

    麻烦LZ还是看一下运行的Tomcat的webapps下是struts的目录结构是什么,理论上应该不会出现WebRoot这个目录的,如果出现了,那么就说明deploy有问题了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法