刚开始学struts 2 ,请教高手,怎么接收jsp表单中传递的数据
action类为
package com.leeyas.Login;import com.opensymphony.xwork2.ActionContext;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{

String username = (String)ActionContext.getContext().getSession().get("username");
if("leeyas"==username){

return "success";
}
else{

return "error";
}

}

}
不管前端输入什么,都会跳转到error.jsp,在线等

解决方案 »

  1.   

    public class loginAction entends ActionSupport先吧然后发你的struts.xml来看看  会不会配置没对?类写的好象没问题我也初学  一起学习吧  呵呵
      

  2.   

    页面的属性名跟username一样就可以在action取值了
    String username = (String)ActionContext.getContext().getSession().get("username"); 
    这个是多余的
      

  3.   

    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="strutsqs" extends="struts-default">
    <action name="login" class="com.leeyas.Login.loginAction">
    <result name="success">/success.jsp</result>
    <result name="error">/error.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>
    <!-- 
    <s:form action="login.action">
    <s:textfield name="username" label="用户名:"/>
    <s:textfield name="password" label="密码:"/>
    <s:submit name="登陆"/>
    </s:form>
     -->
    <form action="login.action" method = "post">

    <input type="text" name="username"><br>
    <input type="text" name="password"><br>
    <input type="submit" name="登录">
    </form>
    </body>
    </html>
      

  4.   

    "leeyas"==username ? 可以这样比较吗? 改为equals 另外String username = (String)ActionContext.getContext().getSession().get("username"); 
    这个确实多余execute方法中直接if("leeyas".equals(username)){ return "success"; 

    else{ return "error"; 
    } } }
      

  5.   

    判断错误:
    用以下方式进行判断
    if("leeyas".equals(username)) { 
       return "success"; 
    } else{ 
       return "error";