Spring web 3.0.5 的 Jetty 7 web.xml 配置
我在 eclipse indigo 中配置了 Jetty 7 嵌入模式。当我在 eclipse 中启动 jetty 时,它似乎没有加载我的 web.xml 和 spring-servlet.xml 配置。我在网上搜索,似乎每个人都在做与我相同的配置,但我的无法加载。我是不是忘记了什么?或者 Jetty 7 配置与 jetty 6 不同?
Main.java
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setResourceBase("../jettyserver/webapp");
context.setContextPath("/");
context.setParentLoaderPriority(true);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{ context, new DefaultHandler()});
server.setHandler(context);
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.jetty.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
I configured a Jetty 7 embedded mode in eclipse indigo. When i start jetty in eclipse it seems not loading my web.xml and spring-servlet.xml configuration. I search on the net, it seems everybody doing the same configuration as me but mine doesn't load. Did i forgot something ? or Jetty 7 configuration its different than jetty 6 ?
Main.java
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setResourceBase("../jettyserver/webapp");
context.setContextPath("/");
context.setParentLoaderPriority(true);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{ context, new DefaultHandler()});
server.setHandler(context);
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
web.xml
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.jetty.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是这里的专家,但如果您使用 eclipse,我会说您的资源库可能有问题。
根据您在此处使用 Eclipse 的方式(WTP、Maven 等),编译后的代码最终将位于目标文件夹中的某个位置。然而,您的 JSP 和所有内容可能不会遵循或可能不在您认为的位置。
编译您的代码并查看目标文件夹,看看它在哪里。我做了类似的事情,但它是测试代码,所以我并不关心路径是否链接到我的 IDE。我们的版本几乎相同,除了应用差异之外,除了以下内容:
正如您在此处所看到的,Eclipse 中应用程序运行时的根路径是您的项目在工作区中的根路径。我设置了我的,所以它发现它是相对于项目的根路径的东西,因此目标在那里。
但是,如果您正在寻找在您的环境中运行并且部署后的代码,您可能可以更改特定应用程序的运行配置中的根文件夹,并将其直接设置为目标(或任何适合您的内容)。
很抱歉在这里没有更加果断,
无论如何,希望这会有所帮助。
Not an expert here but if you are using eclipse I'd say there could be a problem with your resource base.
Depending on how you use Eclipse here (WTP, Maven etc) the compiled code will end up in the target folder somewhere. However your JSP and all may not follow or may not be where you'd think they are.
Compile your code and look in your target folder see where it is. I do something similar but it's testing code so I don't really care if the paths are linked to my IDE. our versions are pretty much identical, applicative differences aside, except for these :
As you can see here the root path of the application run-time within Eclipse is your project's root path in your workspace. I set mine so it finds it's stuff relative to the project's root path, hence the target in there.
If however you are looking for code that runs in both your environment and once deployed you could probably change the root folder in the run configuration for your particular application and set it to target directly (or whatever works for you).
Sorry for not being more decisive here,
Anyways, hope this helps.