jsp (6): Use javaBean to directly embed java files in jsp

Generally, we don't want the jsp code to contain too much complex and functionally mixed java code. At this time, the javabean class comes in handy---you can introduce the written java file into the jsp.

First, create a package in the src directory in java resources. For example, I created a jspDemo package and created a new Student class in it:

package jspDemo;    

//When the external class is defined, the package must be defined, otherwise the java file cannot be found

public class Student { private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

The key code of Student.jsp is as follows. Note that we have added "import=jspDemo.Student" to the page instruction:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import='jspDemo.Student'%>
...
<body>
<%
    Student s=new Student();
    s.setAge(99);
    out.print("s的年龄:"+s.getAge()+"<br>");
    out.print("----");
%>
</body>
...

The result outputs 18.

Note that if you want to wrap the java output in the jsp page, you can't use the regular "\n" or out.println, you can use <br>.

If you have a detailed explanation of the bean syntax in jsp, you should know that Student.jsp can also be written through the jsp instruction:

....
<body>
<jsp:useBean id="s" class="jspDemo.Student"></jsp:useBean>
<jsp:setProperty name="s" property="age" value="99"/>
s的年龄:<jsp:getProperty name ="s" property="age"/>
</body>
...

The effect is the same. If this is written, import can be omitted in the page file.

Special attention, setProperty is not equivalent to the direct initialization parameter age , we can see by viewing the Student_jsp.java file compiled by Student.jsp! setProperty actually calls the setAge(99) method! . The same is true for getProperty, there must be a corresponding get method in the java file.

It is found through experiments that the parameter of jsp is "age", and when "getAge()" exists in Java, the program can run. jsp: "age", java: "getage()", runnable.     

 jsp:"Age" ,java:"getAge"(报错)、 jsp:"Age",java:"getage()"报错。


In general, we want to set the value directly in the browser page instead of setting the parameter value in the server.

So we can get the value through the form form. For example, in the following example, we can find the area of ​​the trapezoid by entering the upper base, lower base, and height:

The code of Tixing.java is as follows:

package jspDemo;

public class Tixing {
	private double a,b,h; //Top bottom, bottom bottom, high
	public double getA() {
		return a;
	}
	public void setA(double a) {
		this.a = a;
	}
	public double getB() {
		return b;
	}
	public void setB(double b) {
		this.b = b;
	}
	public double getH() {
		return h;
	}
	public void setH(double h) {
		this.h = h;
	}
	public double getArea(){
		return ((a+b)*h)/2;
	}
}

jsp is as follows:

....
<body>
<form action="" method="get" >
梯形上底:<input type="text" name="a"><br>
梯形下底:<input type="text" name="b"><br>
梯形的高:<input type="text" name="h"><br>
<input type=submit value="确定">
</form>
<jsp:useBean id="t" class="jspDemo.Tixing"></jsp:useBean>
<jsp:setProperty name="t" property="*" />
The upper bottom of the trapezoid: <jsp:getProperty name="t" property="a"/>
Bottom of the trapezoid: <jsp:getProperty name="t" property="b"/>
The height of the trapezoid: <jsp:getProperty name="t" property="h"/>
The area of ​​the trapezoid: <jsp:getProperty property="area" name="t"/>
</body>
...

Among them, property="*" in setproperty: store all the values ​​entered by the user in Jsp, which are used to match the properties in the Bean (java file). The name of the attribute in the bean must be the same as the parameter name in the request object (that is, the form) .

Note: The value and param properties of setproperty cannot be used at the same time, but either of them can be used. (I will update tomorrow at 4.26, go back to sleep....)

value is the value of the custom attribute, and param is the request parameter (such as front-end form data) injected into the property as a value.

In addition, the value of param should correspond to the name attribute name of the front-end request parameter, indicating which request parameter will be injected into the property.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324935283&siteId=291194637
jsp