action:
package com.user;import com.opensymphony.xwork2.ActionSupport;public class Login extends ActionSupport{
private String name;
private String password;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public String execute(){
if(name=="liu"&&(password=="123")){
return SUCCESS;
}else {
return INPUT;
}
}}
登录页:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=GB18030">
<title>Login</title>
</head>
<body>
<s:form action="Login">
Name: <s:textfield name="name" />
Password:<s:textfield name="password"/>
<s:submit />
</s:form>
</body>
</html>
为什么,不管输入的是什么,返回的页面总是INPUT那个,而从来不返回SUCCESS呢,大家帮帮忙,谢谢啦
package com.user;import com.opensymphony.xwork2.ActionSupport;public class Login extends ActionSupport{
private String name;
private String password;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public String execute(){
if(name=="liu"&&(password=="123")){
return SUCCESS;
}else {
return INPUT;
}
}}
登录页:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=GB18030">
<title>Login</title>
</head>
<body>
<s:form action="Login">
Name: <s:textfield name="name" />
Password:<s:textfield name="password"/>
<s:submit />
</s:form>
</body>
</html>
为什么,不管输入的是什么,返回的页面总是INPUT那个,而从来不返回SUCCESS呢,大家帮帮忙,谢谢啦
public String execute(){
if(name.equals("liu") && password.equals("123")) {
return SUCCESS;
}else {
return INPUT;
不容易发生空指针异常
是的。==比较的是内存地址,equals比较的literal的相等
对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同。
“==”比较两个变量本身的值,即两个对象在内存中的首地址
“equals()”比较字符串中所包含的内容是否相同。
比如:
String s1,s2,s3 = "abc", s4 ="abc" ;
s1 = new String("abc");
s2 = new String("abc");
那么:
s1==s2 是 false //两个变量的内存地址不一样,也就是说它们指向的对象不 一样,
故不相等。