用户对象:
package com.lan.models; import java.io.Serializable; public class User implements Serializable {
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;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
} //action
package com.lan.actions; import com.lan.models.User;
import com.opensymphony.xwork2.Action; public class LoginAction implements Action {
private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} @Override
public String execute() throws Exception { System.out.println("user = " + user);//这里打印user=null
if ("lan".equals(user.getUsername())
&& "lan".equals(user.getPassword())) {
System.out.println("in success");//这里没有打印
return SUCCESS; } else {
System.out.println("in error");//这里也没有打印
return ERROR;
}
} }
//action配置:
<action name="login" class="com.lan.actions.LoginAction">
<result>
/hellos/loginSuccess.jsp
</result>
<result name="error">
/hellos/loginError.jsp
</result>
</action>//用户登录界面:
login.jsp <%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>loginjsp</title>
</head>
<body>
<form action="login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.username"></td>
</tr>
<tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password"></td>
</tr>
<tr>
<td><input type="reset" value="重填"></td>
<td><input type="submit" name="登录"></td>
</tr>
</table>
</form>
</body>
</html> //loginError.jsp提交后老是登录到这个界面来
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>loginErrorjsp</title>
</head>
<body>
<h3>
用户名与密码错误,请重新<a href="login.jsp">登录 </a>
</h3>
</body>
</html>//loginSuccess.jsp<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>loginSuccessjsp</title>
</head>
<body>
<h3>
password:<s:property value="user.password"/> </h3>
</body>
</html>
tomcat打印:
十月 26, 2013 8:34:01 下午 org.apache.jasper.compiler.TldLocationsCache tldScanJar
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
user = null
环境是:apache-tomcat-7.0.42,win7 64
package com.lan.models; import java.io.Serializable; public class User implements Serializable {
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;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
} //action
package com.lan.actions; import com.lan.models.User;
import com.opensymphony.xwork2.Action; public class LoginAction implements Action {
private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} @Override
public String execute() throws Exception { System.out.println("user = " + user);//这里打印user=null
if ("lan".equals(user.getUsername())
&& "lan".equals(user.getPassword())) {
System.out.println("in success");//这里没有打印
return SUCCESS; } else {
System.out.println("in error");//这里也没有打印
return ERROR;
}
} }
//action配置:
<action name="login" class="com.lan.actions.LoginAction">
<result>
/hellos/loginSuccess.jsp
</result>
<result name="error">
/hellos/loginError.jsp
</result>
</action>//用户登录界面:
login.jsp <%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>loginjsp</title>
</head>
<body>
<form action="login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.username"></td>
</tr>
<tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password"></td>
</tr>
<tr>
<td><input type="reset" value="重填"></td>
<td><input type="submit" name="登录"></td>
</tr>
</table>
</form>
</body>
</html> //loginError.jsp提交后老是登录到这个界面来
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>loginErrorjsp</title>
</head>
<body>
<h3>
用户名与密码错误,请重新<a href="login.jsp">登录 </a>
</h3>
</body>
</html>//loginSuccess.jsp<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>loginSuccessjsp</title>
</head>
<body>
<h3>
password:<s:property value="user.password"/> </h3>
</body>
</html>
tomcat打印:
十月 26, 2013 8:34:01 下午 org.apache.jasper.compiler.TldLocationsCache tldScanJar
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
user = null
环境是:apache-tomcat-7.0.42,win7 64
解决方案 »
- 表单有N多字段要插入数据库中,有没有什么偷懒的方法
- 大家来评评这个连接类怎么样???
- 有关门面模式和ResourceBundle的问题,急。
- 请问一个Servlets生成xml文件的问题
- showModalDialog的问题
- new String(request.getParameter("userID").trim().getBytes("8859_1"))的含义是什么?
- 求教关于Javabean使用的问题?我怎么就搞不好呢?很烦人
- 一个有关jasperreport的问题,在pdf文当中无法显示内容?
- 初学jsp碰见的问题
- 变量的使用
- 如何实现一个页面控制另一个页面的刷新问题
- java web项目在运行一段时间之后会出现一直刷新的情况,请看下面的图
配置信息如下:
//struts.xml
<?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"> <default-action-ref name="index" /> <global-results>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
</package> <include file="lan.xml"/> <!-- Add packages here --></struts>
//lan.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="lan" extends="default" > <action name="helloworld" class="com.lan.test.HelloWorldAction">
<result name="success">
/hellos/HelloWorld.jsp
</result>
</action>
<action name="login" class="com.lan.actions.LoginAction">
<result>
/hellos/loginSuccess.jsp
</result>
<result name="error">
/hellos/loginError.jsp
</result>
</action> <!-- Add actions here -->
</package>
</struts>
package com.lan.actions;import com.lan.models.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;public class LoginAction implements Action, ModelDriven<User> {
private User user = new User(); @Override
public String execute() throws Exception { System.out.println("user = " + user);// 这里打印user=null
if ("lan".equals(user.getUsername())
&& "lan".equals(user.getPassword())) {
System.out.println("in success");// 这里没有打印
return SUCCESS; } else {
System.out.println("in error");// 这里也没有打印
return ERROR;
} } @Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}}
//tomcat打印:
user = User [username=null, password=null]
in error//环境中的struts jar包
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
freeer-2.3.19.jar
javassist-3.11.0.GA.jar
log4j-1.2.17.jar
ognl-3.0.6.jar
struts2-core-2.3.15.3.jar
xwork-core-2.3.15.3.jar