java 从按钮调用 JSP 中的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14401267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call a bean in JSP from button
提问by user1851366
How do i call a method from a jsp when i click in a button?
当我单击按钮时,如何从 jsp 调用方法?
I wrote this code,but dont work..
我写了这段代码,但不起作用..
<%@page import="my.package.class.MyClass" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<input type="submit" name="myButton" value="button" onclick="callMethod()"/>
</body>
</html>
<%!
void callMethod(){
new MyClass().print();} %>
there is any way more easy? or this is the right way for to do?
有没有更简单的方法?或者这是正确的做法?
ps: I dont want to use javascript
ps:我不想使用javascript
edit: My class just have a method "print",that prints something like "test" in system.out.println
编辑:我的班级只有一个方法“print”,它在 system.out.println 打印类似“test”的东西
回答by BalusC
You need a servlet( <-- click the link, it isn't included for decoration only).
您需要一个servlet(<-- 单击该链接,它不包含仅用于装饰)。
To the point, provided that you're targeting a Servlet 3.0 container (Tomcat 7, Glassfish 3, etc), then just create this class:
就此而言,假设您的目标是 Servlet 3.0 容器(Tomcat 7、Glassfish 3 等),那么只需创建此类:
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
new MyClass().print();
}
}
And fix your JSP's <body>
as follows (provided that JSP file is placed in root of web content and not in a subfolder):
<body>
并按如下方式修复您的 JSP (假设 JSP 文件位于 Web 内容的根目录而不是子文件夹中):
<form action="hello" method="post">
<input type="submit" name="myButton" value="button" />
</form>
That's it.
而已。
The servlet is via @WebServlet
registered to listen on an URL of /hello
, relative to context root. The HTML form is instructed to submit to exactly that URL, relative to the URL of the JSP page itself. The method="post"
will invoke the servlet's doPost()
method wherein you've all the freedom to write down the desired Java code.
servlet 通过@WebServlet
注册来侦听/hello
相对于上下文根的 URL 。指示 HTML 表单提交到该 URL,相对于 JSP 页面本身的 URL。该method="post"
会调用servlet的doPost()
,其中你都可以自由地写下所需的Java代码的方法。
If you want more finer grained control depending on the button pressed, then give the button an unique name.
如果您想要根据按下的按钮进行更细粒度的控制,请为按钮指定一个唯一名称。
<form action="hello" method="post">
<input type="submit" name="myButton1" value="button1" />
<input type="submit" name="myButton2" value="button2" />
<input type="submit" name="myButton3" value="button3" />
</form>
then you can just check the button pressed as follows in servlet's doPost()
method:
那么你可以在servlet的doPost()
方法中检查按下的按钮:
if (request.getParameter("myButton1") != null) {
// button1 is pressed.
}
else if (request.getParameter("myButton2") != null) {
// button2 is pressed.
}
else if (request.getParameter("myButton3") != null) {
// button3 is pressed.
}
A completely different alternative is to go for an existing MVC framework which abstracts all this boilerplate away in a high degree, such as JSF, Spring MVC, Struts2, etc.
一个完全不同的选择是使用现有的 MVC 框架,该框架高度抽象了所有这些样板,例如JSF、Spring MVC、Struts2等。
With JSF, it would look like this (you only need to replace legacy JSP by its successor Facelets; how Facelets look like is explained in a bit sane JSF tutorial, again, click the "JSF" link in previous paragraph for details):
使用 JSF,它看起来像这样(你只需要用它的后继 Facelets 替换旧的 JSP;Facelets 的样子在一个有点理智的 JSF 教程中进行了解释,同样,单击上一段中的“JSF”链接以获取详细信息):
<h:form>
<h:commandButton value="button1" action="#{bean.button1}" />
<h:commandButton value="button2" action="#{bean.button2}" />
<h:commandButton value="button3" action="#{bean.button3}" />
</h:form>
with just this class without ugly if/elses:
只有这个类没有丑陋的 if/elses:
@ManagedBean
@RequestScoped
public class Bean {
public void button1() {
System.out.println("button1 invoked");
}
public void button2() {
System.out.println("button2 invoked");
}
public void button3() {
System.out.println("button3 invoked");
}
}
回答by Noah Martin
why not use servlets and MVC pattern, you have no need to write java methods within JSP.
为什么不使用 servlets 和 MVC 模式,您无需在 JSP 中编写 java 方法。
回答by Nick Holt
You are confusing where the bits of code involved in a JSP and the resulting HTML, which the browser displays, are running.
您对 JSP 中涉及的代码位和浏览器显示的结果 HTML 在哪里运行感到困惑。
The bean that is accessible inside the JSP is in the servlet container (on the server) at the time the HTTP request is being processed and the HTML is generated from the JSP, while the button that fires the onclick event is inside the clients browser - therefore the button cannot invoke the bean directly.
在处理 HTTP 请求并从 JSP 生成 HTML 时,JSP 内部可访问的 bean 位于 servlet 容器中(在服务器上),而触发 onclick 事件的按钮位于客户端浏览器内部 -因此按钮不能直接调用 bean。
In order to invoke server-side logic when the button is clicked you need to make an AJAX call back to the server (unless you're going to refresh the whole page, which is a bit crap).
为了在单击按钮时调用服务器端逻辑,您需要对服务器进行 AJAX 调用(除非您要刷新整个页面,这有点废话)。