我正在编写一个简单的注册申请。有一个register.jsp(允许用户输入他们的信息),一个JAVABean和一个welcome.jsp(打印出用户信息)。
在welcome.jsp中。我想在我的代码中添加if..else..如果用户在register.jsp中检查同意TOS选项,则打印出他们的信息。此外,还有一个警告,他们必须同意服务条款。
当我写if..else时,我有问题...只是说,我尝试使用:
<% if (<%= User.getTos()%> != null) {
%> Print Information <%
} else {
%> A Warning <%
} %>
但是如果...... else ..有语法错误。我不知道如何解决它。我应该如何正确编写if语句?**
这是register.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>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="welcome.jsp" method="POST">
<table>
<tr><td>Email</td><td><input type="text" name="email"></td></tr>
<tr><td>Full name</td><td><input type="text" name="name"></td></tr>
<tr><td>Password</td><td><input type="password" name="password"></td></tr>
<tr><td>Gender</td><td><input type="radio" name="gender" value="male">Male<br><input type="radio" name="gender" value="female">Female</td></tr>
<tr><td>Favourite colour</td><td><select name="favcol"> <option>red<option>green</select></td></tr>
<tr><td>Agree to TOS</td><td><input type="checkbox" name="tos" value="tos"> </td></tr>
<tr><td></td><td><input type="submit" value="Register"></td></tr>
</table>
</form>
</body>
</html>
这是Java Bean(我是由Eclipse生成的):
package uts;
public class User {
private String name;
private String email;
private String password;
private String gender;
private String favcol;
private String tos;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String email, String password, String gender, String favcol, String tos) {
super();
this.name = name;
this.email = email;
this.password = password;
this.gender = gender;
this.favcol = favcol;
this.tos = tos;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getFavcol() {
return favcol;
}
public void setFavcol(String favcol) {
this.favcol = favcol;
}
public String getTos() {
return tos;
}
public void setTos(String tos) {
this.tos = tos;
}
}
这是welcome.jsp(包含if..else的那个......)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="User" scope="session" class="uts.User"/>
<jsp:setProperty name="User" property="*"/>
<!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 bgcolor="<%= User.getFavcol() %>">
**<% if (<%= User.getTos()%> != null) { %>**
<p>Welcome, <%= User.getName() %>!</p>
<p>Your Email is <%= User.getEmail() %>.</p>
<p>Your password is <%= User.getPassword() %>.</p>
<p>Your gender is <%= User.getGender() %>.</p>
<p>Your favourite colour is <%= User.getFavcol() %>.</p>
<% } else { %>
<p>Sorry, you must agree to the Terms of Services.</p>
<p>Click <a href="register.jsp">here</a> to go back.</p>
<%
}
%>
</body>
</html>
答案 0 :(得分:6)
不要嵌套scriptlet。
<% if (<%= User.getTos()%> != null) { %>
应该是
<% if (User.getTos() != null) { %>
另外,请不要使用scriptlet。
答案 1 :(得分:0)
您无法嵌套“&lt;%”标记。改为:
<% if (User.getTos() != null) { %>
答案 2 :(得分:0)
如果您应该更改代码,则无需要求<%
置于其中:<% if (User.getTos() != null) { %>