IAm*_*aja 2 java servlets web-applications war
我的WAR结构如下:
my-web-app.war/
views/
index.html
blah.html
META-INF/
MANIFEST.MF
WEB-INF/
web.xml
lib/
<!-- Dependencies -->
classes/
org.me.mywebapp.controllers/
MyController.class
<!-- Other packages/classes as well -->
Run Code Online (Sandbox Code Playgroud)
我想配置,web.xml以便在本地部署WAR时,index.html可以通过访问来访问页面http://localhost/my-web-app/index.html.
这是我到目前为止所拥有的:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<!-- The display name of this web application -->
<display-name>My Web App</display-name>
<listener>
<listener-class>
org.me.mywebapp.context.ContextImpl
</listener-class>
</listener>
</web-app>
Run Code Online (Sandbox Code Playgroud)
如何配置此URL以查看映射?提前致谢!
您可以像这样映射您的servlet
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.me.mywebapp.controllers.MyController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>index.html</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>controller2</servlet-name>
<servlet-class>org.me.mywebapp.controllers.OtherController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller2</servlet-name>
<url-pattern>blah.html</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
如果你想将view/blah.html显示为/blah.html,在控制器中你只需将请求发送到适当的views/* .html或jsp或任何你想要的东西.
编辑:正如您所要求的:您可以将请求发送到servlet中的另一个页面,如下所示:
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/views/blah.html");
dispatcher.forward(request, response);
Run Code Online (Sandbox Code Playgroud)
虽然上面的代码工作正常,你应该在每个servlet中实现一个更"复杂"的方法,以决定你将分派哪个视图,特别是如果你的应用程序有很多控制器,视图等等.试试如果你阅读更多有关MVC实现的内容还没有完成.
| 归档时间: |
|
| 查看次数: |
11788 次 |
| 最近记录: |