2014-01-29 49 views
0

我是Spring中的新成員我正在嘗試開發一個Spring應用程序,其中在jsp頁面中將提供有關學生的一些信息,並且當我們按下提交按鈕時,信息將返回另一個jsp頁面,這是我的moto.I已經完成了eclise ide.But,但是當我嘗試運行這個例子時,404即將到來project structure in eclipse來自示例不工作的Spring MVC

我在這裏發佈我的完整代碼。相同的包com.tutorialspoint

Student.java下

web.xml 


<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<display-name>HelloWeb</display-name> 

    <servlet> 
    <servlet-name>HelloWeb</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

<servlet-mapping> 
    <servlet-name>HelloWeb</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
</web-app> 

的HelloWeb-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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.tutorialspoint" /> 

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

</beans> 

bean類和控制器是Bean類

public class Student { 
private Integer age; 
private String name; 
private Integer id; 

public void setAge(Integer age) { 
    this.age = age; 
} 

public Integer getAge() { 
    return age; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getName() { 
    return name; 
} 

public void setId(Integer id) { 
    this.id = id; 
} 

public Integer getId() { 
    return id; 
} 
} 

StudentController。 java是控制器類

在WEB-INF
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.ui.ModelMap; 

    @Controller 
    public class StudentController { 

    @RequestMapping(value = "/student", method = RequestMethod.GET) 
     public ModelAndView student() { 
     return new ModelAndView("student", "command", new Student()); 
    } 

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST) 
    public String addStudent(@ModelAttribute("SpringWeb")Student student, 
    ModelMap model) { 
     model.addAttribute("name", student.getName()); 
     model.addAttribute("age", student.getAge()); 
     model.addAttribute("id", student.getId()); 

     return "result"; 
    } 
} 

這裏的jsp文件夾視圖,我們有兩個頁面有student.jsp & result.jsp中

student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
<title>Spring MVC Form Handling</title> 
</head> 
<body> 

<h2>Student Information</h2> 
<form:form method="POST" action="/HelloWeb/addStudent"> 
<table> 
    <tr> 
    <td><form:label path="name">Name</form:label></td> 
    <td><form:input path="name" /></td> 
</tr> 
<tr> 
    <td><form:label path="age">Age</form:label></td> 
    <td><form:input path="age" /></td> 
</tr> 
<tr> 
    <td><form:label path="id">id</form:label></td> 
    <td><form:input path="id" /></td> 
</tr> 
<tr> 
    <td colspan="2"> 
     <input type="submit" value="Submit"/> 
     </td> 
    </tr> 
</table> 
    </form:form> 
    </body> 
    </html> 

result.jsp中

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
    <title>Spring MVC Form Handling</title> 
</head> 
<body> 

<h2>Submitted Student Information</h2> 
    <table> 
<tr> 
    <td>Name</td> 
    <td>${name}</td> 
</tr> 
<tr> 
    <td>Age</td> 
    <td>${age}</td> 
</tr> 
<tr> 
    <td>ID</td> 
    <td>${id}</td> 
</tr> 
    </table> 
    </body> 
    </html> 

這是完整的代碼,我試過我也加入了Spring庫。但是當我嘗試運行它時,我總是得到一個404錯誤。有人請幫助

+0

哪個要求給你404? –

+0

當我右鍵點擊該項目,然後單擊運行,然後它給 – Subho

回答

0

在您的HelloWeb-servlet.xml中,您需要配置您的MVC環境。做到這一點與

<mvc:annotation-driven /> 

此配置元件寄存器任何豆它發現它是一個@Controller並且具有@RequestMapping方法作爲HTTP請求的處理程序。

您將需要適當的XML名稱空間聲明。

+0

我發佈回正確的HelloWeb-servlet.xml,你請檢查 – Subho

+0

它不工作 – Subho

+0

@Subho'不工作'是永遠不應該你應該單獨發佈的東西。什麼不起作用?它怎麼不起作用?你不期望的是什麼? –

0

這是因爲你正在攔截jsp文件以及使用<url-pattern>/</url-pattern> 所以Resolution就是這樣的。

  1. 在web.xml中使用<url-pattern>*.htm</url-pattern>
  2. 在student.jsp <form:form method="POST" action="/HelloWeb/addStudent.htm">
+0

默認情況下,Tomcat(我認爲是OP是什麼使用)爲擴展名* .jsp註冊'JspServlet'。這應該匹配請求以在匹配「DispatcherServlet」的'/'之前轉發到JSP。 –

0

我通過在該網站上所有的例子在幾個月前的工作,並記住他們。

@Sotirios Delimanolis,我相信只要您使用基於xml的配置和進行組件掃描,註釋驅動標記就不是必需的。 @Controller註釋應該仍然被註冊。您建議的註釋標記不在原始教程中,也不在我的工作示例中(仍在筆記本電腦上),並且仍然運行。另外,它看起來像我教程XML中的所有XML名稱空間聲明都顯示在OP的[servlet-name] -servlet.xml文件中。

@Pankaj Sharma,web.xml中的/表示法是教程中顯示的內容,在我的示例中工作正常,而我的.jsp與action =「/ HelloWeb/addStudent」一起使用。 無論如何,那個jsp動作應該只會影響到'結果'屏幕,並且OP甚至沒有那麼遠。

OP,首先我肯定你會在你的構建路徑中添加教程中所有需要的jar文件。此外,我想知道你是否試圖正確調用你的教程應用程序。從我的MyEclipseBlue IDE中,即使在左鍵單擊並試圖將其作爲「MyEclipse服務器應用程序」運行時,我也無法運行它。整個URL不會出現,它缺少匹配控制器中的模式所需的URL中的/學生。 如果您仔細閱讀示例,它會建議您啓動服務器並打開一個新的瀏覽器窗口,然後輸入完全按照原始教程中所示的URL。這適用於我的應用程序,或者將其作爲「MyEclipse服務器應用程序」運行,然後輸入帶有/ student附加的完整URL。