2016-11-17 62 views
0

我是MVC的新手。爲什麼請求不會從hello.jsp傳輸到控制器?

我想用Spring MVC構建一個簡單的hello world應用程序。 我得到404錯誤,因爲href不會轉到hello.jsp。我在我的WEB-INf和index下有我的hello.jsp,jsp在Web應用下。以下是我的應用程序代碼。謝謝大家提前

@Controller 
public class HelloController { 

    @RequestMapping(value = "/hello",method = RequestMethod.POST) 
    public ModelAndView mymethod(){ 
     return new ModelAndView("hello","msg","Hello First Spring"); 
    } 
} 
/////////////////////////// spring.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:component-scan base-package="com.javatpoint"></context:component-scan> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix" value="/WEB-INF/jsp/"></property> 
<property name="suffix" value=".jsp"></property> 
</bean> 

</beans> 
/////////////////////////  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"> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/.jsp</url-pattern> 
    </servlet-mapping> 

</web-app> 
//////////////////////////// index.jsp  //////////////////////////// 
<html> 
<body> 
<h2>Hello World!</h2> 

<%= java.util.Calendar.getInstance().getTime() %> 

<a href="hello.jsp">click here</a> 

</body> 
</html> 

    //////////////////////////// hello.jsp ///////////////////////// 
    Message is: ${msg}] 


    [1]: https://i.stack.imgur.com/XUJ9j.png 
+0

請至少重新格式化的問題w.r.t.在https://i.stack.imgur.com/XUJ9j.png的截圖鏈接(所以願意幫助的人不必複製粘貼。 – Dilettant

+0

@Rishibian你改變了我告訴你的嗎? – cralfaro

回答

0

更改控制器到達

@RequestMapping(value = "/hello",method = RequestMethod.GET) 
    public ModelAndView mymethod(){ 
     return new ModelAndView("hello","msg","Hello First Spring"); 
    } 

也能改變你的web.xml

<servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/*</url-pattern> 
</servlet-mapping> 
+0

嗨Cralfaro,我試過,但仍然得到相同的404錯誤。我無法從hello.jsp調用hello控制器 –

+0

@RishibanR修改web.xml中的代碼我在響應中添加了代碼 – cralfaro

相關問題