2016-05-29 58 views
0

您能幫我弄清楚如何使用我的控制器調用登錄頁面嗎?Spring MVC簡單控制器示例

這裏是我的代碼:

package com.mvc.demo; 

public class Emp { 
    private String name; 
    private String password; 

    public String getName() { 
     return name; 
    } 

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

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

} 

MvcDemo.java(這是我的控制器;只是調用登錄頁面)

package com.mvc.demo; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

public class MvcDemo { 

    @RequestMapping(value="/login", method = RequestMethod.GET) 
    public String showForm(Emp em) { 
      return "login"; 
    } 
} 

調度-servlet.xml中

<context:component-scan base-package="com.mvc.demo" /> 
<mvc:annotation-driven /> 
<beans> 
<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 
</beans> 

網.xml

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

的login.jsp

<form:form action="#" method = "post" modelAttribute="emp"> 
     <form:label path="username">Enter your user-name</form:label> 
     <form:input id="username" name="username" path="name" /><br> 
     <form:label path="username">Please enter your password</form:label> 

     <form:password id="password" name="password" path="password" /><br> 

     <input type="submit" value="Submit" /> 
</form:form> 

項目結構:

MvcDemo 
    JavaResources 
     src 
     com.mvc.demo 
    WebContent 
     jsp 
     login.jsp 
    WEB-INF 
    lib 
    web.xml 
    dispatcher-servlet.xml 
    index.jsp 
+0

你訪問http://本地主機:8080 /登錄?你有錯誤嗎? – Daniel

+0

非常感謝Joao。請在上面找到我的配置命名爲dispatcher-servlet.xml –

+0

爲什麼不去github,從spring示例https://github.com/spring-projects/spring-mvc-showcase下載工作演示,或者可以使用google https: //www.google.co.in/search?q=github+spring+mvc+hello+world – tgkprog

回答

0

它不一樣在你的代碼,當你點擊網址與本地主機:8080/Mvc_Demo /登錄就必須顯示你的login.jsp, 希望這可以解決你的問題。

package com.mvc.demo; 

    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 

    import org.springframework.web.servlet.ModelAndView; 

    public class MvcDemo { 
    @RequestMapping(value="/login", method = RequestMethod.GET) 

    public String showForm() 
    { 
     ModelAndView mv = new ModelAndView("login"); 
     return mv; 


     } 
    } 
+0

非常感謝Punjan,但它不工作,即使你返回modelandview但方法期待String類型。我已經改變和執行,即使它似乎相同的問題http狀態404錯誤 –

+0

其不工作 –

1

您缺少控制器類上的@Controller註釋。 Spring除非通過使用註釋實例化控制器,否則不會爲url創建處理程序。

+0

謝謝satya仍然得到山姆錯誤HTTP 404 MvcDemo /登錄未找到 –

+0

你看到任何映射/日誌登錄? –

+0

雅如初始化Spring框架servlet'dispatcher' –

0

試試這個代碼:

package com.mvc.demo; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
public class MvcDemo { 

    // To call the view for login 
    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    public ModelAndView login() { 
     return new ModelAndView("login","newEmp", new Emp()); 
    } 

    // To call the validate login after submit 
    @RequestMapping(value = "/user-login", method = RequestMethod.POST) 
    @ResponseBody 
    public ModelAndView userLogin(Emp emp) { 
     //TODO check 'emp' object to validate user 
     return new ModelAndView("home"); 
    } 


}