动态替换 Java Servlet 中的 HTML 代码
Replace HTML code from Java Servlet dynamically
我目前正在学习 Java 使用 JSP 和 Servlet 进行服务器端编程。我创建了一个简单的动态 Web 项目并将其部署在 apache tomcat 中。基本上,只有一个页面(jsp)作为前端,根据用户选择(form/radio 按钮)使用 servlet 动态更改。
The directory structure:
| index.jsp
|
+---img
| apples.jpg
| oranges.jpg
| salad.jpg
| strawberries.jpg
|
\---WEB-INF
| web.xml
|
\---classes
DynamicServlet.class
DynamicServlet.java
我已经配置 web.xml 来启动 index.jsp @(http://localhost:port/contextname)
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
来自 index.jsp 的相关 代码:
...<head>...<style>
.jumbotron {
background-image: linear-gradient( rgba(0, 0, 0, ${grad}), rgba(0, 0, 0, ${grad}) ), url('${pageContext.request.contextPath}/img/${img}');
background-size: cover;
color: ${color};
}
...<body>
<div class="container"> <!-- jumbotron-container -->
<div class="jumbotron">
<h1>Hello, World!</h1> <!-- <====This line====> -->
</div> <!-- /jumbotron -->
</div> <!-- /jumbotron container -->
<div class="container"> <!-- form-container -->
<form action="${pageContext.request.contextPath}/magic" method="get">
<label class="form-label">Pick your favorite:</label>
<div class="input-group form-group col-xs-6">
<span class="input-group-addon">
<input type="radio" name="option" value="apples">
</span>
<input type="text" class="form-control" disabled="disabled" placeholder="Apples">
</div> <!-- /apples -->
<div class="...
</div> <!-- /oranges -->
<div class="...
</div> <!-- /strawberries -->
<div class="...
</div> <!-- /salad -->
<input type="submit" class="btn btn-primary" value="Submit">
</form> <!-- /form -->
</div> <!-- /form-container -->
结果是这样的:
现在,当用户进行选择并提交时,servlet 会根据用户选择使用图像更新超大屏幕封面。如您所见,我为此使用了 EL。
Servlet代码:
...doGet(HttpServletRequest.....{
String option = request.getParameter("option");
try {
switch (option) {
case "apples":
request.setAttribute("img", "apples.jpg");
break;
case "oranges":....and so on
}...
...request.getRequestDispatcher("index.jsp").forward(request, response);
它按预期工作。我无法弄清楚的是如何根据选择 CHANGE 超大屏幕文本。我正在寻找一种方法,根据用户从servlet 最好使用 getRequestDispatcher()。有什么实现这一目标的提示吗?
注:
1. 着陆页必须有静态超大屏幕文字(Hello, World!)
2. 更改必须反映在同一页面上。我不打算创建一个
分开 jsp.
我是初学者,所以如果我错过了一个明显的解决方案,请体谅。谢谢。
我想你的整个问题最终可以归结为这样的事情:
I would like to show a static default value when an EL expression does not evaluate to anything.
在这种情况下,请在条件表达式中使用 empty
运算符。
<h1>${empty title ? 'Hello World' : title}</h1>
那么这只是 servlet 中的下面一行的问题,以防您想更改它。
request.setAttribute("title", "Not a Hello World");
您甚至可以将其完全保留在视图端,因为您基本上对名称为 option
.
的请求参数的值感兴趣
<h1>${param.option eq 'apples' ? 'Yay, Apples!' : param.option eq 'oranges' ? 'Yay, Oranges!' : 'Hello World'}</h1>
我目前正在学习 Java 使用 JSP 和 Servlet 进行服务器端编程。我创建了一个简单的动态 Web 项目并将其部署在 apache tomcat 中。基本上,只有一个页面(jsp)作为前端,根据用户选择(form/radio 按钮)使用 servlet 动态更改。
The directory structure:
| index.jsp
|
+---img
| apples.jpg
| oranges.jpg
| salad.jpg
| strawberries.jpg
|
\---WEB-INF
| web.xml
|
\---classes
DynamicServlet.class
DynamicServlet.java
我已经配置 web.xml 来启动 index.jsp @(http://localhost:port/contextname)
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
来自 index.jsp 的相关 代码:
...<head>...<style>
.jumbotron {
background-image: linear-gradient( rgba(0, 0, 0, ${grad}), rgba(0, 0, 0, ${grad}) ), url('${pageContext.request.contextPath}/img/${img}');
background-size: cover;
color: ${color};
}
...<body>
<div class="container"> <!-- jumbotron-container -->
<div class="jumbotron">
<h1>Hello, World!</h1> <!-- <====This line====> -->
</div> <!-- /jumbotron -->
</div> <!-- /jumbotron container -->
<div class="container"> <!-- form-container -->
<form action="${pageContext.request.contextPath}/magic" method="get">
<label class="form-label">Pick your favorite:</label>
<div class="input-group form-group col-xs-6">
<span class="input-group-addon">
<input type="radio" name="option" value="apples">
</span>
<input type="text" class="form-control" disabled="disabled" placeholder="Apples">
</div> <!-- /apples -->
<div class="...
</div> <!-- /oranges -->
<div class="...
</div> <!-- /strawberries -->
<div class="...
</div> <!-- /salad -->
<input type="submit" class="btn btn-primary" value="Submit">
</form> <!-- /form -->
</div> <!-- /form-container -->
结果是这样的:
现在,当用户进行选择并提交时,servlet 会根据用户选择使用图像更新超大屏幕封面。如您所见,我为此使用了 EL。
Servlet代码:
...doGet(HttpServletRequest.....{
String option = request.getParameter("option");
try {
switch (option) {
case "apples":
request.setAttribute("img", "apples.jpg");
break;
case "oranges":....and so on
}...
...request.getRequestDispatcher("index.jsp").forward(request, response);
它按预期工作。我无法弄清楚的是如何根据选择 CHANGE 超大屏幕文本。我正在寻找一种方法,根据用户从servlet 最好使用 getRequestDispatcher()。有什么实现这一目标的提示吗?
注:
1. 着陆页必须有静态超大屏幕文字(Hello, World!)
2. 更改必须反映在同一页面上。我不打算创建一个
分开 jsp.
我是初学者,所以如果我错过了一个明显的解决方案,请体谅。谢谢。
我想你的整个问题最终可以归结为这样的事情:
I would like to show a static default value when an EL expression does not evaluate to anything.
在这种情况下,请在条件表达式中使用 empty
运算符。
<h1>${empty title ? 'Hello World' : title}</h1>
那么这只是 servlet 中的下面一行的问题,以防您想更改它。
request.setAttribute("title", "Not a Hello World");
您甚至可以将其完全保留在视图端,因为您基本上对名称为 option
.
<h1>${param.option eq 'apples' ? 'Yay, Apples!' : param.option eq 'oranges' ? 'Yay, Oranges!' : 'Hello World'}</h1>