使用注释在spring mvc中创建多个页面

时间:2013-06-05 11:19:00

标签: jsp spring-mvc spring-annotations

我想使用注释方法在spring mvc中创建一个示例项目,用户可以通过单击按钮导航到一个页面到另一个页面。我是spring mvc的新手,我为单页显示创建了一个示例项目

我期待你的帮助。 提前致谢

Login.java

 package credentials;

 public class Login {

private String name;
private String password;

public void setName(String name){
    this.name=name;
}
public String getName(){
    return name;
}

public void setPassword(String password){
    this.password=password;
}
public String getPassword(){
    return password;
}
  }

Logincontroller.java

     package credentials;

    import org.apache.tomcat.jdbc.pool.Validator;
      import org.springframework.stereotype.Controller;
       import org.springframework.ui.ModelMap;
     import org.springframework.validation.BindingResult;
      import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
     import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;


     @Controller
    public class LoginController {

@RequestMapping(value="/login")
public ModelAndView login(){
    return new ModelAndView("login","command",new Login());

}



@RequestMapping(value="/calc")
public ModelAndView Add(){
    return new ModelAndView("calc","command",new Login());

}


    }

的login.jsp

       <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <title>Insert title here</title>
   </head>
    <body>
    <h2>Login here</h2>
    <form:form method="POST" action="calc">
    <table>
    <tr>
     <td><form:label path="name">Name</form:label></td>
      <td><form:input path="name" /> </td> 
     </tr>
     <tr>
     <td><form:label path="password">Password</form:label></td>
     <td><form:password path="password"></form:password></td>

      </tr>
       <tr>
       <td colspan="2">
         <input type="submit" value="login" />
       </td>  

       </table>

calc.jsp          &lt;%@ taglib uri =“http://www.springframework.org/tags/form”prefix =“form”%&gt;

     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
       <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
         <title>Insert title here</title>
         </head>
         <body>
         <form:form method="POST" action="content">
         <h2>Add content</h2>
         <table>

         <tr>
         <form:button path="mouse" value="Mouse">Mouse</form:button>

        <td>

            </tr>
       </table>
      </form:form>
      </body>
      </html>

content.jsp

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
       <html>
       <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <title>Insert title here</title>
      </head>
       <body>
      <h2>Submitted Student Information</h2>


        </body>
          </html>

单击计算页面上的“鼠标”按钮时,会发生以下错误

      HTTP Status 404 - 

    type Status report

    message 

   description The requested resource is not available.

添加

    <form:input path="mouse"/>

点击登录页面中的登录按钮时出现以下错误

         HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/jsp/calc.jsp  at line 16

     type Exception report

       message An exception occurred processing JSP page /WEB-INF/jsp/calc.jsp at line 16

        description The server encountered an internal error that prevented it from fulfilling this request.

例外

            org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/calc.jsp at line 16

        13: 
        14: <tr>
        15:   <form:button path="mouse" value="Mouse">Mouse</form:button>
        16:    <form:input path="mouse"/>
        17: <td>
        18: 
        19: </tr>


   Stacktrace:
       org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


 root cause 

    org.springframework.beans.NotReadablePropertyException: Invalid property 'mouse' of     bean class [credentials.Login]: Bean property 'mouse' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:729)
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:721)
org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:219)
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
org.apache.jsp.WEB_002dINF.jsp.calc_jsp._jspx_meth_form_005finput_005f0(calc_jsp.java:201)
org.apache.jsp.WEB_002dINF.jsp.calc_jsp._jspx_meth_form_005fform_005f0(calc_jsp.java:126)
org.apache.jsp.WEB_002dINF.jsp.calc_jsp._jspService(calc_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

1 个答案:

答案 0 :(得分:0)

有很多示例项目已经在Spring上为github提供了这个。

查看他们的存储库here

所以你想要的只是点击一个按钮,这会激活一个返回视图的弹簧端点。