我用的是struts2.1.6一、domainpackage com.sun.domain;public class User {
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;
}
}
二Action
package com.sun.action;import com.opensymphony.xwork2.ActionSupport;
import com.sun.domain.User;
//Struts2一个请求一个Action 解决线程死锁的问题
public class LoginAction extends ActionSupport {
private User user;
    public LoginAction() {
     System.out.println("LoginAction构造器被调用");
    }
 
public String execute() throws Exception {
System.out.println(user.getUsername());
System.out.println(user.getPassword());
                   return SUCCESS;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
}}
三、JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
-->
  <s:head />
  </head>
  
  <body>
    <s:form action="login" method="post" theme="simple">
     <table>
     <tr>
         <td>
          用户名:
         </td>
     <td><s:textfield name="username"></s:textfield></td>
     <td>
          密码:
         </td>
     <td><s:password name="password"></s:password></td>
     </tr>
    
     <tr>
     <td><s:submit value="登陆" /></td>
     <td><s:reset value="重置" /></td>
     </tr>
     </table>
    </s:form>
  </body>
</html>
四、struts.xml
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
-->
  <s:head />
  </head>
  
  <body>
    <s:form action="login" method="post" theme="simple">
     <table>
     <tr>
         <td>
          用户名:
         </td>
     <td><s:textfield name="username"></s:textfield></td>
     <td>
          密码:
         </td>
     <td><s:password name="password"></s:password></td>
     </tr>
    
     <tr>
     <td><s:submit value="登陆" /></td>
     <td><s:reset value="重置" /></td>
     </tr>
     </table>
    </s:form>
  </body>
</html>
请问为什么在Action中System.out.println(user.getUsername());
System.out.println(user.getPassword());取不到用户名和密码的值?

解决方案 »

  1.   

    不好意思,上面struts.xml文件复制错了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="login" namespace="/" extends="struts-default">
            
            <action name="login" class="com.sun.action.LoginAction">
                <result name="success">success.jsp</result>
                 <result name="login">login.jsp</result>
            </action>
        </package>
    </struts>
      

  2.   

    <td><s:textfield name="username"></s:textfield></td>应该是这样的><s:textfield name="userus.ername"></s:textfield>
      

  3.   

    <td><s:textfield name="username"></s:textfield></td>应该是这样的><s:textfield name="user.username"></s:textfield>
      

  4.   

    我刚学的时候也遇到过这样的问题改成:
    <tr>
    <td>
    用户名:
    </td>
    <td><s:textfield name="user.username"></s:textfield></td>
    <td>
    密码:
    </td>
    <td><s:password name="user.password"></s:password></td>
    </tr>
    就可以了。