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">
 
 <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> 
    </servlet>
 
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
 
 <welcome-file-list>
     <welcome-file>form.jsp</welcome-file>
   </welcome-file-list>
</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.InternalResourceView</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="commandClass">
            <value>onlyfun.caterpillar.LoginForm</value>
        </property>
        <property name="successView">
            <value>success</value>
        </property>
        <property name="formView">
            <value>form</value>
        </property>
    </bean>
</beans>


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

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

public class LoginAction extends SimpleFormController {
    protected ModelAndView onSubmit(Object command) throws Exception {
       LoginForm form = (LoginForm) command;
       
       if("111".equals(form.getUsername()) &&
             "111".equals(form.getPassword())) {
          return new ModelAndView(this.getSuccessView(),"user", form.getUsername());
       }
       else {
           return new ModelAndView(this.getFormView());
       }
    }
}

4./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;
    }
}


5./form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>Login</title></head>
<body>
    請輸入使用者名稱與密碼:<p>
    <form name="loginform" action="login.do" method="post">
        名稱 <input type="text" name="username"/><br>
        密碼 <input type="password" name="password"/><br>
        <input type="submit" value="確定"/>
    </form>
    注意:輸入錯誤會再回到這個頁面中。
</body>
</html>


6./success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>Login Success</title></head>
<body>
    <H1> Hello, ${user}!!</H1>
    This is your secret gift.
</body>
</html>

posted on 2006-08-23 16:29 software5168 阅读(498) 评论(0)  编辑  收藏 所属分类: Java學習


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


网站导航:
 
<2006年8月>
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

收藏夹

JAVA學習網站

綜合

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜