创建一个视图JSP文件的helloWorld.jsp

让我们创建下面的JSP文件的helloWorld.jsp,在WebContent文件夹在你的eclipse项目。要做到这一点,右键单击WebContent文件夹中的项目资源管理器,然后选择“新建”>“JSP文件。该文件将被要求的情况下,返回的结果是success,这是一个字符串常量“成功”的定义在Action接口:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   Hello World, <s:property value="name"/>
</body>
</html>

以下是文件,该文件将被调用的框架的情况下作用的结果是等于字符串常量“ERROR”的错误,这是。以下内容的AccessDenied.jsp的

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Access Denied</title>
</head>
<body>
   You are not authorized to view this page.
</body>
</html>

我们还需要在WebContent文件夹中创建的index.jsp。该文件将作为初始动作URL,用户可以直接点击告诉Struts 2框架,以调用execute方法HelloWorldAction类,并呈现HelloWorld.jsp视图。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h1>Hello World From Struts2</h1>
   <form action="hello">
      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="Say Hello"/>
   </form>
</body>
</html>

就是这样,有没有需要改变的web.xml文件,让我们使用同一个web.xml举例章节,我们已经创建了。现在,我们已经准备好来运行我们的Hello World应用程序使用Struts 2框架。

原文地址:https://www.cnblogs.com/borter/p/9502141.html