springMVC第四個例子

1./WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
-->
 
 <!--
 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/application-servlet.xml</param-value>
   </context-param>
 <listener>
     <listener-class>
       org.springframework.web.context.ContextLoaderListener
     </listener-class>
   </listener>
   -->
 <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/application-servlet.xml</param-value>
      </init-param>
  <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
 
<!--
 <taglib>
      <taglib-uri>/spring</taglib-uri>
      <taglib-location>/WEB-INF/spring.tld</taglib-location>
 </taglib>
--> 
</web-app>

2./WEB-INF/application-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/login.do">loginAction</prop>
            </props>
        </property>
    </bean>
   
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>
        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
   
    <bean id="loginAction" class="onlyfun.caterpillar.LoginAction">
        <property name="commandName">
         <value>command</value>
  </property>
  <property name="commandClass">
            <value>onlyfun.caterpillar.LoginForm</value>
        </property>
        <property name="successView">
            <value>success</value>
        </property>
        <property name="formView">
            <value>form</value>
        </property>
       <property name="user">          
            <ref local="user" />
        </property>
    </bean> 
   
   <bean id="user" class="onlyfun.caterpillar.User">
     <property name="username">
         <value>111</value>
     </property>
     <property name="password">
         <value>111</value>
     </property>
    </bean>
</beans>



3./form.jsp
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>Login</title></head>
<body>
 <spring:bind path="command.*">
        <font color="red"><b>${status.errorMessage}</b></font><br>
    </spring:bind>
       請輸入使用者名稱與密碼:<p>    
    <form name="loginform" action="login.do" method="post">
     <spring:bind path="command.username">  
          名稱 <input type="text" name="${status.expression}" value="${status.value}"/>
          <font color="red">${status.errorMessage}</font><br>
     </spring:bind>
     <spring:bind path="command.password"> 
          密碼 <input type="password" name="${status.expression}" value="${status.value}"/>
          <font color="red">${status.errorMessage}</font><br>
     </spring:bind>
        <input type="submit" value="確定"/>
    </form>
      注意:輸入錯誤會再回到這個頁面中。  
</body>
</html>

4./index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>  
    begin......
    <%
     response.sendRedirect("login.do");
    %>
  </body>
</html>

5./success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head><title>Login Success</title></head>
<body>
    <H1><c:out value="用戶名﹕${user}"/></H1>
</body>
</html>

6./WEB-INF/classes/onlyfun/caterpillar/LoginAction.class
package onlyfun.caterpillar;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.*;

public class LoginAction extends SimpleFormController { 
    private User user;
    protected ModelAndView onSubmit(Object command,BindException errors) throws Exception {
       LoginForm form = (LoginForm) command; 
       String username = user.getUsername();
       String password = user.getPassword();
       if(username.equals(form.getUsername()) &&
         password.equals(form.getPassword())) {
          return new ModelAndView(this.getSuccessView(),"user", form.getUsername());
       }
       else {
        errors.reject("loginfail", "使用者名稱或密碼錯誤");
        if(!(username.equals(form.getUsername()))){
         errors.rejectValue("username", "error", null, "使用者名稱錯誤");
        }
       if(!(password.equals(form.getPassword()))){
         errors.rejectValue("password", "error", null, "密碼錯誤");
        }      
           return new ModelAndView(this.getFormView(),errors.getModel());
       }
    }
    public User getUser() {
     return user;
    }
    public void setUser(User user) {
     this.user = user;
    }
}



7./WEB-INF/classes/onlyfun/caterpillar/LoginForm.class
package onlyfun.caterpillar;

public class LoginForm {
    private String username;
    private String password;
   
    public void setUsername(String username) {
       this.username = username;
    }
   
    public void setPassword(String password) {
       this.password = password;
    }
   
    public String getUsername() {
       return username;
    }
   
    public String getPassword() {
       return password;
    }
}

8./WEB-INF/tags/spring.tld

9./WEB-INF/tags/c.tld

10./WEB-INF/classes/onlyfun/caterpillar/User.class
package onlyfun.caterpillar;

public class User {
 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;
 }
}

posted on 2006-09-06 18:03 software5168 阅读(1388) 评论(4)  编辑  收藏 所属分类: Java學習

评论

# re: springMVC第四個例子 2006-09-06 19:48 12.194.210.9

看不懂啊  回复  更多评论   

# re: springMVC第四個例子 2006-11-15 10:35 guest

这是一个很不错的入门篇,如果在有注释的话就更完美了,就是不知道能否正常发布运行了?
试试先……  回复  更多评论   

# re: springMVC第四個例子 2007-08-06 10:33 qiqnzi

xiexie
  回复  更多评论   

# re: springMVC第四個例子 2007-09-10 12:05 WJC

很不错,是练习SpringFramework的好例子!!!!!!!!^V^  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2007年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

收藏夹

JAVA學習網站

綜合

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜