[英]Spring MVC Simple controller example
Can you please help me figure out how to invoke the login page by using my controller? 您能帮我弄清楚如何使用控制器来调用登录页面吗?
Here is my code: 这是我的代码:
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 (it's my controller; just for invoking login page) 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";
}
}
dispatcher-servlet.xml dispatcher-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 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>
login.jsp 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>
project Structure: 项目结构:
MvcDemo
JavaResources
src
com.mvc.demo
WebContent
jsp
login.jsp
WEB-INF
lib
web.xml
dispatcher-servlet.xml
index.jsp
You are missing @Controller annotation on your controller class. 您在控制器类上缺少@Controller批注。 Spring does not create a handler for url unless you instantiate a controller by using annotation.
除非您使用注释实例化控制器,否则Spring不会为url创建处理程序。
It does the same as in your code,when you hit url with localhost:8080/Mvc_Demo/login it must show your login.jsp, hope this solves your problem. 它的作用与代码相同,当您使用localhost:8080 / Mvc_Demo / login击url时,它必须显示您的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;
}
}
Try this code: 试试这个代码:
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.