Skip to content

Commit f6438eb

Browse files
authored
[call-class-in-jsp] call class in jsp (#18521)
* [call-class-in-jsp] call class in jsp * [call-class-in-jsp] fix typo
1 parent 1dabe9d commit f6438eb

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.boot.jsp.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
7+
@Controller
8+
@RequestMapping("/course")
9+
public class WelcomeController {
10+
11+
@GetMapping("/welcome")
12+
public String greetingAndWelcome() {
13+
return "course/welcome";
14+
}
15+
16+
@GetMapping("/welcome-usebean")
17+
public String greetingAndWelcomeUseBean() {
18+
return "course/welcome-usebean";
19+
}
20+
21+
@GetMapping("/welcome-by-javabean")
22+
public String greetingAndWelcomeByJavaBean() {
23+
return "course/welcome-by-javabean";
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.boot.jsp.coursewelcome;
2+
3+
public class CourseWelcome {
4+
5+
public String greeting(String username) {
6+
return String.format("Hi %s, how are you doing?", username);
7+
}
8+
9+
public static String staticWelcome(String courseName) {
10+
return String.format("Welcome to Baeldung's %s course", courseName);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.boot.jsp.coursewelcome;
2+
3+
public class CourseWelcomeBean {
4+
5+
private String username;
6+
private String courseName;
7+
8+
public String getUsername() {
9+
return username;
10+
}
11+
12+
public void setUsername(String username) {
13+
this.username = username;
14+
}
15+
16+
public String getCourseName() {
17+
return courseName;
18+
}
19+
20+
public void setCourseName(String courseName) {
21+
this.courseName = courseName;
22+
}
23+
24+
public String greetingUser() {
25+
return String.format("Hi %s, how do you do?", username);
26+
}
27+
28+
public String welcomeMsg() {
29+
return String.format("Welcome to Baeldung's %s course!", courseName);
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
<head>
4+
<title>Welcome to Course</title>
5+
</head>
6+
<body>
7+
<p>Using jsp:useBean action with a JavaBean</p>
8+
<jsp:useBean id="courseWelcomeBean" class="com.baeldung.boot.jsp.coursewelcome.CourseWelcomeBean"/>
9+
<jsp:setProperty name="courseWelcomeBean" property="username" value="Eric"/>
10+
<jsp:setProperty name="courseWelcomeBean" property="courseName" value="Spring Security"/>
11+
<div><%= courseWelcomeBean.greetingUser()%></div>
12+
<div><%= courseWelcomeBean.welcomeMsg()%></div>
13+
</body>
14+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<head>
3+
<title>Welcome to Course</title>
4+
</head>
5+
<body>
6+
<p>Using jsp:useBean action</p>
7+
<jsp:useBean id="welcomeBean" class="com.baeldung.boot.jsp.coursewelcome.CourseWelcome"/>
8+
<div>
9+
<%= welcomeBean.greeting("Kevin")%>
10+
</div>
11+
<div>
12+
<%= com.baeldung.boot.jsp.coursewelcome.CourseWelcome.staticWelcome("Java Collections")%>
13+
</div>
14+
</body>
15+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<%@ page import="com.baeldung.boot.jsp.coursewelcome.CourseWelcome" %>
3+
<html>
4+
<head>
5+
<title>Welcome to Course</title>
6+
</head>
7+
<body>
8+
<%
9+
CourseWelcome courseWelcomeObj = new CourseWelcome();
10+
%>
11+
<div><%= courseWelcomeObj.greeting("Kai")%></div>
12+
<div><%= CourseWelcome.staticWelcome("Spring Boot")%></div>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)