问题描述
我使用maven创建的Web应用程序存在问题.我使用RESTEasy在此应用程序中创建了一些Web服务,并且我想在此Web应用程序中创建带有表单的简单html页面,以测试@post @FormParam批注.我在Web应用程序的"webapp"文件夹中创建了login.html页面.
I have a problem with my web app created with maven. I use RESTEasy to create some webservices in this app and I want to create a simple html page with a form in this webapp in order to test the @post @FormParam annotations. I created the login.html page located in the 'webapp' folder of my webapplication.
但是当我尝试访问/resteasyWebapp/login.html上的页面或/resteasyWebapp/index.jsp的默认jsp页面时,它不起作用.我收到404错误代码.我看了看我的应用程序包,这两个文件(login.html和index.jsp)都位于我的应用程序的根目录下.我的网络服务正常,但是我无法通过chrome/firefox访问html页面.
but when I tried to reach the page at /resteasyWebapp/login.html or the default jsp page at /resteasyWebapp/index.jsp, it doesn't work. I get a 404 error code. I took a look at the package of my application and the both files (login.html and index.jsp) are located at the root of my application.My webservice works but I'm not able to reach the html page through chrome/firefox...
项目层次结构:此处
login.html
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login example</title>
</head>
<body>
<form method="POST" action="login">
<table>
<tr>
<td>Email Address:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>
我的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spanier.resteasy</groupId>
<artifactId>resteasyWebapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>resteasyWebapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<!-- Obligatoire pour fournir les bons providers -->
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.4.FINAL</version>
</dependency>
<!-- XML provider -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.4.FINAL</version>
</dependency>
<!-- JSON provider -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>3.0.4.FINAL</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Pour conteneur servlet 3.0 // jetty 8.x ou tomcat, preconisé par la
doc -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.4.Final</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
</plugin>
</plugins>
</pluginManagement>
</build>
我的web.xml:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>My RestEasy sample Web Application</display-name>
<!-- <listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class> </listener> -->
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>HelloWorldApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldApplication,我在其中定义我的根资源:
the HelloWorldApplication, where I define my root resources:
public class HelloWorldApplication extends Application {
private Set<Object> singletons = new HashSet();
private Set<Class<?>> empty = new HashSet();
public HelloWorldApplication() {
// ADD YOUR RESTFUL RESOURCES HERE
this.singletons.add(new HelloWorld());
this.singletons.add(new LoginService());
}
public Set<Class<?>> getClasses()
{
return this.empty;
}
public Set<Object> getSingletons()
{
return this.singletons;
}
}
我认为RESTEasy的配置缺少一些东西,但我不知道是什么...
I think there is something I missing with the configuration of RESTEasy but I don't figure out what...
推荐答案
如果要在应用程序中提供html或jsp页面,则不应将<url-pattern>
映射到/*
.如果您这样做,Resteasy将拦截对您页面的请求,并且由于它不是REST服务,因此将不会显示该页面.解决方案是为您的服务URL添加前缀.
If you want to serve html or jsp pages in your application, you shouldn´t map <url-pattern>
to /*
. If you do so, Resteasy will intercept the request for your page and, since it is not a rest service, the page will not be displayed. The solution is to add a prefix do your services URLs.
将<url-pattern>/*</url-pattern>
更改为<url-pattern>/service/*</url-pattern>
并添加
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/service</param-value>
</context-param>
到您的web.xml.还记得在调用Web服务时添加"/service"前缀.这样一来,您应该能够访问页面,并对应用程序提供的各种内容进行逻辑分隔.
to your web.xml. Also remember to add the "/service" prefix when you call your webservices. That way you should be able to access your pages and also have a logical separation for the different kinds of content the your application provides.
这篇关于从Maven RESTEasy Webapp在tomcat中显示HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!