Spring MVC Simple控制器示例

时间:2016-05-29 18:58:31

标签: java spring spring-mvc

您能帮我解决一下如何使用我的控制器调用登录页面吗?

这是我的代码:

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";
     } 
}

MvcDemo.java(它是我的控制器;仅用于调用登录页面)

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

调度-servlet.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>

的web.xml

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

的login.jsp

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

项目结构:

{{1}}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

它与您的代码中的相同,当您使用localhost:8080 / Mvc_Demo / login访问url时,它必须显示您的login.jsp, 希望这能解决你的问题。

 if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {


    } else if (shouldShowRequestPermissionRationale(
            Manifest.permission.READ_EXTERNAL_STORAGE)) {
        Toast.makeText(this, "Gallery permission is needed to edit photos.",
                Toast.LENGTH_SHORT).show();
    }

    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

    // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
    // app-defined int constant

    return;

答案 2 :(得分: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");
    }


}