Skip to content

Commit eec88c8

Browse files
committed
添加 jsp 单元测试
1 parent fe72bc6 commit eec88c8

File tree

25 files changed

+548
-61
lines changed

25 files changed

+548
-61
lines changed

__release/solon-jakarta-bundle1/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<module>../../solon-jakarta-projects/solon-server/solon-server-jetty-add-websocket-jakarta</module>
3232

3333
<module>../../solon-jakarta-projects/solon-server/solon-server-undertow-jakarta</module>
34+
<module>../../solon-jakarta-projects/solon-server/solon-server-undertow-add-jsp-jakarta</module>
3435

3536
<module>../../solon-jakarta-projects/solon-server/solon-server-tomcat-jakarta</module>
3637
<module>../../solon-jakarta-projects/solon-server/solon-server-tomcat-add-jsp-jakarta</module>

solon-jakarta-projects/solon-server/solon-server-jetty-add-jsp-jakarta/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
<description>Java project for solon</description>
1919

2020
<dependencies>
21+
<dependency>
22+
<groupId>org.noear</groupId>
23+
<artifactId>solon-server-jetty-jakarta</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.noear</groupId>
28+
<artifactId>solon-view-jsp-jakarta</artifactId>
29+
</dependency>
30+
2131
<dependency>
2232
<groupId>jakarta.servlet</groupId>
2333
<artifactId>jakarta.servlet-api</artifactId>
@@ -47,6 +57,18 @@
4757
<artifactId>jakarta.el-api</artifactId>
4858
<version>6.0.1</version>
4959
</dependency>
60+
61+
<dependency>
62+
<groupId>org.noear</groupId>
63+
<artifactId>solon-logging-simple</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.noear</groupId>
69+
<artifactId>solon-test</artifactId>
70+
<scope>test</scope>
71+
</dependency>
5072
</dependencies>
5173

5274
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package features.jetty.jsp;
2+
3+
import org.noear.solon.Solon;
4+
import org.noear.solon.annotation.Controller;
5+
import org.noear.solon.annotation.Mapping;
6+
import org.noear.solon.core.handle.ModelAndView;
7+
8+
/**
9+
*
10+
* @author noear 2025/12/1 created
11+
*
12+
*/
13+
@Controller
14+
public class App {
15+
public static void main(String[] args) {
16+
Solon.start(App.class, args);
17+
}
18+
19+
@Mapping("/")
20+
public ModelAndView home() {
21+
ModelAndView model = new ModelAndView("jsp.jsp");
22+
model.put("title","dock");
23+
model.put("msg","你好 world! in XController");
24+
25+
return model;
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2017-2025 noear.org and authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package features.jetty.jsp;
17+
18+
19+
import jakarta.servlet.jsp.JspException;
20+
import jakarta.servlet.jsp.tagext.TagSupport;
21+
22+
import java.io.IOException;
23+
24+
public class FooterTag extends TagSupport {
25+
@Override
26+
public int doStartTag() throws JspException {
27+
try {
28+
StringBuffer sb = new StringBuffer();
29+
sb.append("<div>").append("你好 world!").append("</div>");
30+
pageContext.getOut().write(sb.toString());
31+
} catch (IOException e) {
32+
throw new JspException(e);
33+
}
34+
35+
return super.doStartTag();
36+
}
37+
38+
@Override
39+
public int doEndTag() throws JspException {
40+
return super.doEndTag();
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package features.jetty.jsp;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.noear.solon.test.HttpTester;
5+
import org.noear.solon.test.SolonTest;
6+
7+
/**
8+
*
9+
* @author noear 2025/12/1 created
10+
*
11+
*/
12+
@SolonTest(App.class)
13+
public class JettyJspTest extends HttpTester {
14+
@Test
15+
public void case1() {
16+
assert path("/").get().contains("你好");
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3+
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
4+
<%@ taglib prefix="ct" uri="/tags" %>
5+
<!DOCTYPE html>
6+
<html>
7+
<head>
8+
<title>${title}</title>
9+
</head>
10+
<body>
11+
jsp::${msg};i18n::${i18n.get("login.title")}
12+
13+
<ct:footer />
14+
</body>
15+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5+
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
6+
7+
<description>自定义标签库</description>
8+
<tlib-version>1.1</tlib-version>
9+
<short-name>ct</short-name>
10+
<uri>/tags</uri>
11+
12+
<tag>
13+
<name>footer</name>
14+
<tag-class>features.jetty.jsp.FooterTag</tag-class>
15+
<body-content>empty</body-content>
16+
</tag>
17+
18+
</taglib>

solon-jakarta-projects/solon-server/solon-server-jetty-jakarta/src/main/java/org/noear/solon/server/jetty/JettyServerAddJsp.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919
import org.eclipse.jetty.ee11.servlet.ServletContextHandler;
2020
import org.eclipse.jetty.ee11.servlet.ServletHolder;
2121
import org.eclipse.jetty.util.resource.PathResourceFactory;
22+
import org.noear.solon.Solon;
23+
import org.noear.solon.core.AppClassLoader;
24+
import org.noear.solon.core.runtime.NativeDetector;
2225
import org.noear.solon.core.util.ResourceUtil;
2326
import org.noear.solon.server.jetty.jsp.JspLifeCycle;
2427
import org.noear.solon.server.jetty.jsp.JspTldLocator;
2528

2629
import jakarta.servlet.ServletContext;
2730
import jakarta.servlet.descriptor.TaglibDescriptor;
31+
import org.noear.solon.server.util.DebugUtils;
2832

33+
import java.io.File;
34+
import java.io.FileNotFoundException;
2935
import java.io.IOException;
36+
import java.net.MalformedURLException;
3037
import java.net.URL;
3138
import java.net.URLClassLoader;
3239
import java.util.Map;
@@ -41,8 +48,8 @@ protected ServletContextHandler buildHandler() throws IOException {
4148
ServletContextHandler handler = getServletHandler();
4249

4350
//jsp 资源的根目录
44-
PathResourceFactory rf = new PathResourceFactory();
45-
handler.setBaseResource(rf.newResource(ResourceUtil.getResource("/")));
51+
String resRoot = getResourceRoot();
52+
handler.setBaseResourceAsString(resRoot);
4653

4754
addJspSupport(handler);
4855
addTdlSupport(handler.getServletContext());
@@ -84,4 +91,50 @@ private void addTdlSupport(ServletContext servletContext) throws IOException {
8491
}
8592
}
8693
}
94+
95+
private String getResourceRoot() throws FileNotFoundException {
96+
URL rootURL = getRootPath();
97+
98+
if (rootURL == null) {
99+
if (NativeDetector.inNativeImage()) {
100+
return "";
101+
}
102+
103+
throw new FileNotFoundException("Unable to find root");
104+
}
105+
106+
if (Solon.cfg().isDebugMode() && Solon.cfg().isFilesMode()) {
107+
File dir = DebugUtils.getDebugLocation(AppClassLoader.global(), "/");
108+
if (dir != null) {
109+
return dir.toURI().getPath();
110+
}
111+
}
112+
113+
return rootURL.getPath();
114+
}
115+
116+
private URL getRootPath() {
117+
URL root = ResourceUtil.getResource("/");
118+
if (root != null) {
119+
return root;
120+
}
121+
122+
try {
123+
URL temp = ResourceUtil.getResource(""); //有些环境,/ 取不到根
124+
if (temp == null) {
125+
return null;
126+
}
127+
128+
String path = temp.toString();
129+
if (path.startsWith("jar:")) {
130+
int endIndex = path.indexOf("!");
131+
path = path.substring(0, endIndex + 1) + "/";
132+
} else {
133+
return null;
134+
}
135+
return new URL(path);
136+
} catch (MalformedURLException e) {
137+
return null;
138+
}
139+
}
87140
}

solon-jakarta-projects/solon-server/solon-server-tomcat-add-jsp-jakarta/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
<groupId>org.noear</groupId>
2121
<artifactId>solon-server-tomcat-jakarta</artifactId>
2222
</dependency>
23+
24+
<dependency>
25+
<groupId>org.noear</groupId>
26+
<artifactId>solon-view-jsp-jakarta</artifactId>
27+
</dependency>
28+
2329
<dependency>
2430
<groupId>jakarta.servlet</groupId>
2531
<artifactId>jakarta.servlet-api</artifactId>
@@ -43,5 +49,17 @@
4349
</exclusion>
4450
</exclusions>
4551
</dependency>
52+
53+
<dependency>
54+
<groupId>org.noear</groupId>
55+
<artifactId>solon-logging-simple</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.noear</groupId>
61+
<artifactId>solon-test</artifactId>
62+
<scope>test</scope>
63+
</dependency>
4664
</dependencies>
4765
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package features.tomcat.jsp;
2+
3+
import org.noear.solon.Solon;
4+
import org.noear.solon.annotation.Controller;
5+
import org.noear.solon.annotation.Mapping;
6+
import org.noear.solon.core.handle.ModelAndView;
7+
8+
/**
9+
*
10+
* @author noear 2025/12/1 created
11+
*
12+
*/
13+
@Controller
14+
public class App {
15+
public static void main(String[] args) {
16+
Solon.start(App.class, args);
17+
}
18+
19+
@Mapping("/")
20+
public ModelAndView home() {
21+
ModelAndView model = new ModelAndView("jsp.jsp");
22+
model.put("title","dock");
23+
model.put("msg","你好 world! in XController");
24+
25+
return model;
26+
}
27+
}

0 commit comments

Comments
 (0)