コントローラを使用してログインページを呼び出す方法を理解できますか?ここで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>
プロジェクト構造:あなたはローカルホストでURLを打ったとき
MvcDemo
JavaResources
src
com.mvc.demo
WebContent
jsp
login.jsp
WEB-INF
lib
web.xml
dispatcher-servlet.xml
index.jsp
あなたは、httpにアクセスしている:// localhostを:8080 /ログイン?エラーが発生していますか? – Daniel
ありがとうJoao。上記のdispatcher-servlet.xmlという名前のコンフィグレーションを見つけてください –
githubにアクセスしないでください。春のサンプルhttps://github.com/spring-projects/spring-mvc-showcaseからデモをダウンロードしたり、Googleのhttps: //www.google.co.in/search?q=github+spring+mvc+hello+world – tgkprog