简体   繁体   中英

How do I call a methods in an jsp file from a javabean?

I have a jsp file with some xml data inside which is linked to an xslt stylesheet.I have several classes, user, users, testJAXB, and DiaryApplication.

My question is how do I call user method or class that will allow me to instead of typing out strings in between the xml tags like joe@average.com I want to be able to do something like this

<% user.getEmail(); %> meaning I can use scriplets to invoke data instead of typing it. How do I do this.

//////////////Below is my JSP file.

<%@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>

/////// Below is two Classes user and Users

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;
}

}

////// Below is my DiaryApplication class

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();
}
}

/////////////////Below is my TestJAXB class

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);
 }
}

In a well-designed MVC application, JSP shouldn't call business logic (Java classes), but should just receive values to display. In other words, it should be aware just of the logic necessary to present received data.

By the way, if you want to proceed in this way you can do it.

To use a Java method in a JSP file you have to:

  • import your class ad the beginning of the file, eg <%@ page import="full.package.path.ClassName" %>
  • then you can directly use its static method in your scriptlet, eg <% ClassName.staticMethod(params...) %>
  • if method you need isn't static you need to construct an instance, then to use the method, eg <% ClassName c = new ClassName(); c.staticMethod(params...) %> <% ClassName c = new ClassName(); c.staticMethod(params...) %>

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM