您能幫我弄清楚如何使用我的控制器調用登錄頁面嗎?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
你訪問http://本地主機:8080 /登錄?你有錯誤嗎? – Daniel
非常感謝Joao。請在上面找到我的配置命名爲dispatcher-servlet.xml –
爲什麼不去github,從spring示例https://github.com/spring-projects/spring-mvc-showcase下載工作演示,或者可以使用google https: //www.google.co.in/search?q=github+spring+mvc+hello+world – tgkprog