2013-04-28 376 views
0

我試圖運行一個示例Spring應用程序,但我無法配置它。我搜查了我的問題,但是對我來說,每件事情都很好。在com.fyp.ptma.controller彈簧配置

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>Spring Hello World</display-name> 
    <welcome-file-list> 
     <welcome-file>/</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>springDispatcher</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-context.xml</param-value> 
     </init-param>   
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>springDispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

控制器文件

@Controller 
public class HelloWorldController { 

    @RequestMapping("/") 
    public String hello() { 
     return "hello"; 
    } 

    @RequestMapping(value = "/hi", method = RequestMethod.GET) 
    public String hi(@RequestParam("name") String name, Model model) { 
     String message = "Hi " + name + "!"; 
     model.addAttribute("message", message); 
     return "hi"; 
    } 

} 

彈簧的context.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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     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.fyp.ptma.controller" /> 
    <mvc:annotation-driven /> 

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

</beans> 

的hello.jsp在WEB-INF /views/hello.jsp

<html> 
<head> 
<title>Home</title> 
</head> 
<body> 
    <h1>Hello World!</h1> 

<hr/> 

    <form action="hi"> 
     Name: <input type="text" name="name"> <input type="submit" value="Submit"> 
    </form> 

</body> 
</html> 
在Tomcat中

ptma.xml文件在的conf /卡塔利娜/本地主機/ ptma.xml

<?xml version="1.0" encoding="utf-8"?> 
<Context docBase="/Volumes/DataDrive/FYP/TestMonkeyAppWeb‬/WebContent‬‪" debug="0" crossContext="true" reloadable="true" > 
</Context> 

訪問我的項目爲localhost:8080/PTMA/ 但它說,HTTP狀態404和tomcat日誌似乎工作精細。

+0

您的表單中沒有方法屬性。 – 2013-04-28 06:31:47

+0

形式屬性關注一些事情時,我提交表單,我的問題是我的網頁甚至不會開放 – 2013-04-28 10:57:20

回答

1

web.xml中的歡迎文件映射看起來不正確。將其更改爲像index.jsp之類的東西,在您的應用的根目錄中創建一個具有此名稱的文件並將其重定向到您的第一個控制器。

+0

我有我的web內容的index.jsp文件/ index.jsp中,但web內容/ WEB-INF/hello.jsp中內部文件,不會開放當我瀏覽localhost時:8080/ptma/hello它表示HTTP狀態404 – 2013-04-28 10:59:47

+0

是的,您已經將jsp視圖映射到「/ WEB-INF/views /」,因此需要將它們存儲在該目錄中。 – Stefan 2013-04-28 11:01:36

+0

對不起,我糾正他們在WEB-INF /意見/不WEB-INF/ – 2013-04-28 11:02:49