我用的是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());取不到用户名和密码的值?
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());取不到用户名和密码的值?
解决方案 »
- dom4j写xml文件自动生成xmlns=""属性的问题
- 怎们能够当我点击按钮的时候 跳转到我指定的页面 为什么下面的已经改变了值,也不能跳转那
- spring+hibernate配置问题
- jsf框架怎么样?
- 跪求一个类似的管理系统,用XML作为数据存储,开发采用tomcat+jsp
- 怎么在div中显示word中的图片
- 用dom4j里面Document.selectNodes()不好用呢![求助]
- 大家现在开发Struts应用的时候都用的什么工具开发的!来者给分!
- 各位大虾.斑竹帮帮忙 介绍点开发EJB的中文资料
- 一个老生常谈的话题
- 请问怎么样让JBOSS在启动时加载一些配置文件,然后在Java程序中读取内容
- 关于struts2 的action中接收来自jsp页面数据的问题,有经验的来看看
<?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>
<tr>
<td>
用户名:
</td>
<td><s:textfield name="user.username"></s:textfield></td>
<td>
密码:
</td>
<td><s:password name="user.password"></s:password></td>
</tr>
就可以了。