填写JSP表单 [英] Populate JSP Form

查看:92
本文介绍了填写JSP表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次调用JSP时如何填充表单?页面刷新后,我需要处理XML文件.填充JSP页面中的表单?(不使用框架)

How to populate a form when JSP is called first time ? I need to process an XML file when a page is refreshed & populate the form in the JSP page?(not using frameworks)

推荐答案

您有2个选项:

  1. 使用<jsp:usebean>,让Bean填充到构造函数中.

  1. Use <jsp:usebean>, let the bean fill itself in the constructor.

<jsp:useBean id="bean" class="com.example.Bean" />
...
<input name="foo" value="${fn:escapeXml(bean.foo)}">
<input name="bar" value="${fn:escapeXml(bean.bar)}">

fn:escapeXml()对于该功能不是必需的,但是如果要在HTML页面中重新显示用户控制的输入,则必须防止XSS攻击.

The fn:escapeXml() is not mandatory for the functioning, but it's mandatory to prevent XSS attacks if you're redisplaying user-controlled input in a HTML page.

使用Servlet的doGet()方法.

Use the doGet() method of a Servlet.

Bean bean = new Bean();
request.setAttribute("bean", bean);
request.getRequestDispatcher("page.jsp").forward(request, response);

,并使用URL到浏览器地址栏中的servlet,而不是JSP的URL.您可以在JSP页面中使用与上述相同的代码,但需jsp:useBean行.

and use the URL to servlet in browser address bar instead of the JSP's one. You can use the same code in JSP page as above, expect of the jsp:useBean line.

方法1更像是​​老派和学生的方式.方式2更加面向MVC,在这种特殊情况下更可取,因为您似乎要做的不仅仅是填充bean.

Way 1 is more the oldschool and student's way. Way 2 is more MVC oriented and preferred in this particular case since you seem to be doing more than just populating a bean.

  • JSP tag info page - contains useful tutorial links at bottom

这篇关于填写JSP表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆