struts2 struts-2.5.8-all 跳转404

a79461350 2017-01-19 03:12:42
萌新,新学习struts2,跳转后报404,百度了1天多,还是得不到解决,特此请教。。
上代码
User.java

package login;
public class User {
// 创建变量 username和password,跟login.jsp页面里面的要保持一致
private String username;
private String password;

// 创建变量后要使用get和set函数,在其他类就可以使用set和get函数进行调用
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() {
return "success";
}
}


struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

<include file="struts-default.xml"></include>
<!-- 指定默认编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 指定需要Struts2处理的请求后缀 -->
<!-- <constant name="struts.action.extension" value="do,action"></constant> -->
<!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->
<constant name="struts.serve.static.browserCache" value="false"></constant>
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 开发模式下使用,可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true"></constant>
<!-- action全部用注解进行配置 -->

<!-- 是否开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!--添加包 -->
<package name="User" namespace="/" extends="struts-default">
<action name="login" class="login.User" method="User">
<result name="success">/result.jsp</result>

</action>
</package>
</struts>

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--定义用户,账号是username,密码是password -->
<form action="login">
账号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" name="submit" value="登录">
<input type="reset" value="重置" >
</form>
</body>
</html>

result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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">


</head>

<body>
username:${requestScope.username } <br>
password:${requestScope.password }
</body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>login</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>

</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


login.jsp页面能出来,但是点击提交后,就404了。控制台也没报错
...全文
115 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zmzimpl 2017-01-19
  • 打赏
  • 举报
回复
咱们找的估计还是同一个,因为我看咱们struts.xml的注释都一样的
a79461350 2017-01-19
  • 打赏
  • 举报
回复
好了! 就是你刚刚说的那个地方导致的。这个复制粘贴的不靠谱啊~~ 我是在网上找的,基本上都是照着打的
zmzimpl 2017-01-19
  • 打赏
  • 举报
回复
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class> Ctrl再点击链接能跳转吗,怎么我的是 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
a79461350 2017-01-19
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

<include file="struts-default.xml"></include>
<!-- 指定默认编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 指定需要Struts2处理的请求后缀 -->
<constant name="struts.action.extension" value="do,action"></constant>
<!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->
<constant name="struts.serve.static.browserCache" value="false"></constant>
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 开发模式下使用,可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true"></constant>
<!-- action全部用注解进行配置 -->

<!-- 是否开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!--添加包 -->
<package name="User" namespace="/" extends="struts-default" strict-method-invocation="false">
<action name="login" class="login.User" method="execute">
<result name="success">/result.jsp</result>

</action>
</package>
</struts>




还是不行~~~
zmzimpl 2017-01-19
  • 打赏
  • 举报
回复
package加上strict-method-invocation="false"试试?
a79461350 2017-01-19
  • 打赏
  • 举报
回复
试了下,还是不行啊。我改成了 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--定义用户,账号是username,密码是password  -->
<form action="login.action">
账号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" name="submit" value="登录">
<input type="reset" value="重置" >
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	
		<include file="struts-default.xml"></include>
		<!-- 指定默认编码集 -->
		<constant name="struts.i18n.encoding" value="UTF-8"></constant>
		<!-- 指定需要Struts2处理的请求后缀 -->
 		<constant name="struts.action.extension" value="do,action"></constant> 
		<!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->
		<constant name="struts.serve.static.browserCache" value="false"></constant>
		<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->
		<constant name="struts.configuration.xml.reload" value="true"></constant>
		<!-- 开发模式下使用,可以打印出更详细的错误信息 -->
		<constant name="struts.devMode" value="true"></constant>
		<!-- action全部用注解进行配置 -->

		<!-- 是否开启动态方法调用 -->
		<constant name="struts.enable.DynamicMethodInvocation" value="false" />
		<!--添加包 -->
		<package name="User" namespace="/" extends="struts-default">  
        <action name="login" class="login.User" method="execute">  
            <result name="success">/result.jsp</result>  
           
        </action>  
    </package>  
	</struts>
wing12332 2017-01-19
  • 打赏
  • 举报
回复
<action name="login" class="login.User" method="User"> <result name="success">/result.jsp</result> </action> 很明显action 错了. 方法指定错了,User是一个类 不是方法 ,将User改为execute
zmzimpl 2017-01-19
  • 打赏
  • 举报
回复
对了,你的版本是2.58的话最好把struts.xml里面的package最好写成<package name="test" namespace="/" extends="struts-default" strict-method-invocation="false">,我的是2.5.5的必须加上strict-method-invocation="false"
zmzimpl 2017-01-19
  • 打赏
  • 举报
回复
<!-- <constant name="struts.action.extension" value="do,action"></constant> --> 这里的注释去掉变成 <constant name="struts.action.extension" value="do,action"></constant> 然后login.jsp页面form表单的action改为action="login.action" 试试,我也刚学
a79461350 2017-01-19
  • 打赏
  • 举报
回复
a79461350 2017-01-19
  • 打赏
  • 举报
回复
自己先顶一下,,沙发。

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧