Html 如何编写包含保存、提交、查看按钮的jsp 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18009589/
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
how can i write a jsp page that include save , submit, view buttons?
提问by Srinivas Thanneeru
i want write a jsp page which must have 3 buttons like $save$ to save the process we have done, to delete, to view what we have done.
我想写一个jsp页面,它必须有3个按钮,比如$save$来保存我们已经完成的过程,删除,查看我们所做的。
i tried as :
<form action="go_save" method="post"> // how to go_view.jsp, go_delete.jsp
<input type="submit" name="submit" value="SUBMIT">
code :
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<form action="go.jsp" method="post" >
name<input type="text" name="name">
age<input type="text" name="age">
<input type="button" value="Save" name="Save"
onclick="document.forms[0].action = 'go_save.jsp'; return true;" />
<input type="button" value="view" name="view"
onclick="document.forms[0].action = 'go_view.jsp'; return true;" />
</form>
</body>
</html>
but here the page is not redirecting to the given pages like go_view, go_save. i want to catch these values name, age in go_view, go_save . but how ?
但这里的页面没有重定向到给定的页面,如 go_view、go_save。我想在 go_view, go_save 中捕获这些值名称、年龄。但是怎么样?
回答by Gopinath Koothaperumal
You can do something like this...have an onclick event
你可以做这样的事情......有一个onclick事件
<button type="button" onclick="location = 'go_save.jsp'">Save</button>
<button type="button" onclick="location = 'go_view.jsp'">Save</button>
remove the "action="go_save""
删除“动作=“go_save””
回答by Rohit Jain
You should do this job in a controller, probably a Servlet rather than the JSP.
您应该在控制器中完成这项工作,可能是一个 Servlet 而不是 JSP。
Create a Servlet(say, RedirectServlet
- you can naem), and in doPost
method, redirect to the relevant JSP page, based on what button was clicked. You can give each button some name for this task.
创建一个 Servlet(比如,RedirectServlet
你可以 naem),并在doPost
方法中,根据点击的按钮重定向到相关的 JSP 页面。您可以为此任务为每个按钮命名。
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("GoSave") != null) {
request.getRequestDispatcher("/go_save.jsp").forward(request, response);
} else if (request.getParameter("GoDelete") != null) {
request.getRequestDispatcher("/go_delete.jsp").forward(request, response);
} else if (request.getParameter("GoView") != null) {
request.getRequestDispatcher("/go_view.jsp").forward(request, response);
}
}
Now, from your form, instead of redirecting to a jsp page, redirect to this servlet:
现在,从您的表单,而不是重定向到一个 jsp 页面,重定向到这个 servlet:
<form action="redirectServlet" method="post">
name<input type="text" name="name">
age<input type="text" name="age">
<input type="submit" name="GoView" value="SUBMIT">
<input type="submit" name="GoDelete" value="SUBMIT">
<input type="submit" name="GoSave" value="SUBMIT">
</form>
Now, you can get the value of name
and age
is respective JSP page, where the servlets redirected the request to, using request.getParameter("name")
.
现在,你可以得到的价值name
,并age
为各自的JSP页面,其中的servlet重定向请求,使用request.getParameter("name")
。
Or you can also use some pattern like - MVC's Front Controller Pattern
或者你也可以使用一些模式,比如 - MVC's Front Controller Pattern