我是春天的初学者mvc我有2个jsp: - 1. Webcontent / index.jsp:这很好用。索引文件具有超链接文本,如: -
<a href="hello.html" rel="nofollow">Say Hello</a>
WebContent / WEB-INF / jsp / hello.jsp:在正文中显示以下内容
${message}
Project Container如下: -
@Controller
public class HelloWorldContainer {
@RequestMapping(value="/hello", method=RequestMethod.GET)
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello.jsp", "message", message);
}
}
以下是WebContent / WEB-INF / web.xml文件: -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>*.*</url-pattern>
</servlet-mapping>
WebContent / WEB-INF / spring-servlet.xml: -
<context:component-scan
base-package="org.explorear.ar" />
<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>
我的问题: - 当我从eclipse在tomcat服务器上运行这个项目时,索引文件出现了 非常好。但是由于索引文件中的文本被超链接到hello.html,我不断获取 Http状态404.
答案 0 :(得分:0)
您将在此行返回整个页面名称
返回新的ModelAndView(“hello.jsp”,“message”,message);
您正在使用ViewResolver,其中您使用.jsp为返回值添加后缀。所以在我的建议中使用这个
返回新的ModelAndView(“hello”,“message”,message);
答案 1 :(得分:0)
我可能会遗漏一些东西,但我认为
org.springframework.web.context.ContextLoaderListener
需要在web.xml中提供和context-param
答案 2 :(得分:0)
这就是你应该如何使用web.xml。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<display-name>Spring3MVC</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>