gpt4 book ai didi

java - Spring MVC 简单 Controller 示例

转载 作者:行者123 更新时间:2023-12-01 09:52:35 24 4
gpt4 key购买 nike

您能帮我弄清楚如何使用我的 Controller 调用登录页面吗?

这是我的代码:

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(这是我的 Controller ;仅用于调用登录页面)

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>

web.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>

登录.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

最佳答案

您的 Controller 类上缺少 @Controller 注释。 Spring 不会为 url 创建处理程序,除非您使用注释实例化 Controller 。

关于java - Spring MVC 简单 Controller 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37513719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com