我有一个带有一些xml数据的jsp文件,里面链接到xslt样式表。我有几个类,user,users,testJAXB和DiaryApplication。
我的问题是如何调用用户方法或类,这样我就可以在xml标签之间输入字符串,例如joe@average.com。我希望能够做到这样的事情
<%user.getEmail(); %GT;意思是我可以使用scriplets调用数据而不是键入数据。我该怎么做。
//////////////以下是我的JSP文件。
<%@page contentType="Application/xml"%><?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="main.xsl"?>
<% String filePath = application.getRealPath("WEB-INF/users.xml"); %>
<jsp:useBean id="diaryApp" class="anypackage.DiaryApplication" scope="application">
<jsp:setProperty name="diaryApp" property="filePath" value="<%=filePath%>"/>
</jsp:useBean>
<% ??? ???? ???%>
<users>
<user>
<email>user</email>
<username>average user</username>
<password>blahblah</password>
</user>
<user>
<email>joe@bloggs.com</email>
<username>Joe Bloggs</username>
<password>foobar</password>
</user>
<user>
<email>Average@joehotmail.com</email>
<username>joe average</username>
<password>password</password>
</user>
<user>
<email>user@email.com</email>
<username>user</username>
<password>password</password>
</user>
</users>
///////以下是两个Classes用户和用户
package anypackage;
import java.util.*;
import java.io.Serializable;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "users")
public class Users implements Serializable {
// The list of user elements does NOT have an extra wrapper element.
// See the comment in the XML file, and compare to the bookshop example.
@XmlElement(name = "user")
private ArrayList<User> list = new ArrayList<User>();
public ArrayList<User> getList() {
return list;
}
public void addUser(User user) {
list.add(user);
}
public void removeUser(User user) {
list.remove(user);
}
public User login(String email, String password) {
// For each user in the list...
for (User user : list) {
if (user.getEmail().equals(email) && user.getPassword().equals(password))
return user; // Login correct. Return this user.
}
return null; // Login incorrect. Return null.
}
}
package anypackage;
import java.io.Serializable;
import java.util.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class User implements Serializable{
@XmlElement(name = "email")
private String email;
@XmlElement(name = "username")
private String username;
@XmlElement(name = "password")
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String email, String username, String password) {
this.email = email;
this.username = username;
this.password = password;
}
public String getEmail() {
return email;
}
public void setName(String email) {
this.email = email;
}
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;
}
}
//////以下是我的DiaryApplication类
package anypackage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class DiaryApplication {
private String filePath;
private Users users;
public DiaryApplication(String filePath, Users users) {
super();
this.filePath = filePath;
this.users = users;
}
public DiaryApplication() {
super();
// TODO Auto-generated constructor stub
}
@XmlElement
public String getFilePath() {
return filePath;
}
@XmlElement
public void setFilePath(String filePath) throws JAXBException, IOException {
this.filePath = filePath;
// This is the file path given to us.
// We should use it
// Load the users from the XML file...
JAXBContext jc = JAXBContext.newInstance(Users.class);
Unmarshaller u = jc.createUnmarshaller();
FileInputStream fin = new FileInputStream(filePath); // use the given file path
users = (Users)u.unmarshal(fin); // This loads the "users" object
fin.close();
}
@XmlElement
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
@XmlElement
public void saveUsers() throws JAXBException, IOException {
JAXBContext jc = JAXBContext.newInstance(Users.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
FileOutputStream fout = new FileOutputStream(filePath);
m.marshal(users, fout);
fout.close();
}
}
/////////////////以下是我的TestJAXB类
package anypackage;
import java.util.*;
import java.io.*;
import javax.xml.bind.*;
public class TestJAXB implements Serializable {
public static void main(String[] args) throws Exception {
Users users = new Users();
users.addUser(new User("randomegue@askdm.com", "tervor", "blahblah", "male", "green"));
users.addUser(new User("joe@bloggs.com", "Joe Bloggs", "foobar", "male", "yellow"));
// Boilerplate code to convert objects to XML...
JAXBContext jc = JAXBContext.newInstance(Users.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(users, System.out);
}
}
答案 0 :(得分:0)
在设计良好的MVC应用程序中,JSP不应该调用业务逻辑(Java类),而应该只接收要显示的值。 换句话说,它应该只知道提供接收数据所需的逻辑。
顺便说一下,如果你想以这种方式继续下去,你就可以做到。
要在JSP文件中使用Java方法,您必须:
<%@ page import="full.package.path.ClassName" %>
<% ClassName.staticMethod(params...) %>
<% ClassName c = new ClassName(); c.staticMethod(params...) %>
希望有所帮助