SFD2008 in GuiZhou University Eddan

Friday Apr 17, 2009

    There is a good image which directly shows Spring Integration Model .

    

     Open netbeans,choose new "project--javaWeb",then click "next" twice,at the last step,select "Spring Web MVC" frame.Now,a simple spring frame has been built in your netbeans IDE.

     This time ,i practise to develop a login demo.First of all , build a jsp page named login.jsp and write following code in this page.

    <body>
        <form name="form1" action="login.do" method="POST">
            <table border="1" width="200">
                    <tr>
                        <td colspan="2">登录窗口</td>
                    </tr>
                    <tr>
                        <td>用户名</td>
                        <td><input type="text" name="username" size="10"></td>
                    </tr>
                    <tr>
                        <td>密码</td>
                        <td><input type="password" name="password" size="10"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" name="submit" value="登录"><a href="register.do?method=init">注册新用户</a></td>
                    </tr>
            </table>

        </form>
    </body>

   besides,create a form class which is a orginal JavaBean class and contains relative variable  of  login.jsp.

public class LoginForm {
    private String username;
    private String password;

    public String getPassword() {
        return password;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    } 
}

     For the more,create a new login control named LoginAction.java .

 protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{
        LoginForm loginForm=(LoginForm)command;

        if(isValid(loginForm)){
            request.getSession().setAttribute(Constants.USERNAME_KEY,loginForm.getUsername());
            return new ModelAndView(getSuccessView());
        }else{
            Map modle=errors.getModel();
            modle.put("loginForm", loginForm);
            return new ModelAndView(getFormView(),modle);
        }
    }

    private boolean isValid(LoginForm loginForm) {
        if(loginForm.getUsername().equals("admin") || loginForm.getPassword().equals("admin")){
            return true;
        }else{
            return false;
        }
    }

 At last,configure controller mapping and controller.

   controller mapping

<bean id="loginMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/login.do">loginAction</prop>
            </props>
        </property>
    </bean>

controller

    <bean id="loginAction"
    class="com.demo.spring.actions.LoginAction">
        <property name="commandClass">
            <value>com.demo.spring.forms.LoginForm</value>
        </property>
        <!-- return the failure page -->
        <property name="formView">
            <value>login</value>
        </property>
        <!--return the success page-->
        <property name="successView">
            <value>welcome</value>
        </property>
    </bean>

    Enjoy your spring tour~~

   Reference:

http://www.ibm.com/developerworks/cn/java/wa-spring1/spring_framework.gif

《开发者突击:java web主流框架整合开发 j2ee+Structs+Hibernate+Spring》 刘中兵编著  ,电子工业出版社

  

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed