困扰我一天的struts第一个程序

冰蓝色狂想曲 2015-05-13 09:53:41
我想做一个简单的登录功能,一共有三个页面,全部放在\WebContent文件夹下,分别是
index.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
index body
</body>
</html>


login.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>login</title>
</head>

<body>
<s:form action="login" namespace="/">
<s:textfield key="username"/>
<s:password key="password" />
<s:submit/>
</s:form>
</body>
</html>


welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>welcome</title>
</head>
<body>
welcome
</body>
</html>


两个配置文件web.xml,放在WebContent\WEB-INF文件夹下

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">

<display-name>Struts Blank</display-name>

<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>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


</web-app>


struts.xml,在编译后自动生成到/WEB-INF\classes文件夹中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

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


</struts>


一个处理函数LoginAction.java

package com.emmashomes.struts2.action;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport{

private static final long serialVersionUID = 1L;

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 excute() {
System.out.println(" LoginAction excute() ");
if( "123".equals(username) && "123".equals(password) ) {
System.out.println(SUCCESS);
return SUCCESS;
}
System.out.println(LOGIN);
return LOGIN;
}
}


我想要的结果是,打开login.jsp页面时,当输入帐号密码都为123时,则进入welcome.jsp页面;否则进入login.jsp页面。
现在的问题是,当打开logIn.jsp页面时:
1)会出现 警告: Missing key [username] in bundles [[org/apache/struts2/struts-messages, com/opensymphony/xwork2/xwork-messages]]!
2)不管用户名密码输入什么,都会进入welcome.jsp页面
3)函数中的System.out.println()语句完全没有执行。

我知道这个问题对于大神们很简单 真心求教 但新手入门 真心痛苦 求教
...全文
123 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39420799 2019-03-04
  • 打赏
  • 举报
回复
<result name="success">welcome.jsp</result>
<result name="login">login.jsp</result>
你struts.xml 里路径名前面没“/ ”能访问?
Spring_Day_Day 2016-06-29
  • 打赏
  • 举报
回复
第一个问题 struts.xml文件中需要指定全局国际化资源文件 <constant name="struts.custom.i18n.resources" value="mess"/>
踏 浪 2015-05-13
  • 打赏
  • 举报
回复
Execute方法名写错了
Bugsbunnys 2015-05-13
  • 打赏
  • 举报
回复
分就给我吧,嘿嘿
冰蓝色狂想曲 2015-05-13
  • 打赏
  • 举报
回复
自己找到 execute()函数名写错

67,515

社区成员

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

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