我的java mvc

來源:互聯網
上載者:User

標籤:

mint mvc 並不是我原創的。她的基礎是廖雪峰老師的webwind mvc。 webwind是廖老師模仿spring的一個 rest 風格的 mvc 架構,功能簡單,但是mvc的核心功能基本具備了,我覺得它的設計思想也很好,很適合想瞭解mvc的學習者。我在他的基礎上做了如下修改:
1、改善了url參數的匹配方式,原來支援9個url參數(1-9),現在支援是個url參數(0-9)。修改之後主要帶來兩個好處。第一:支援url參數個數增多一個;第二:符合電腦的計數方式,代碼中的很多數組不用做特殊的處理,簡化了代碼. 
2、對一些邏輯較複雜的類進行了拆分.
3、支援自動擷取請求參數並封裝到bean內,供action方法使用.目前支援的類型包括:數群組類型,基礎類型的封裝類,普通model類(包含的屬性暫時只支援基礎類型及其封裝類,而且必須有對應的setter方法).如果一個action方法包含了基礎類型(非封裝類),該方法就不是合法的action方法,這是為了避免反射調用action方法時基礎型別參數不能被賦予null值得權宜之舉. 
 4、可以聲明一個action方法只能處理特定方法的請求,目前只支援GET和POST方法. 
?1. [代碼]一個典型action類如下     

package demo;
import annotation.Get;
import annotation.Mapping;
import annotation.Param;
import annotation.Post;
 
@Mapping("/user")
public class UserAction {
    @Mapping("/login")
    public String hello(@Param("phone") String phone,@Param("user")User user) {
        return "<h1>Hello, " + phone + "</h1>";
    }
     
    @Get
    @Mapping("/register")
    public String register(Integer a){
        return "<h1></h1>";
    }
     
    @Post
    @Mapping("/confirm/$0")/*$+paramindex*/
    public void confirm(String name, @Param("hobby")String[] hobby, @Param("user")User user){
        System.out.println(name);英雄聯盟高清壁紙
         
        if( hobby != null){
            for(String s : hobby){
                System.out.println(s);
            }
        }
         
        System.out.println("name:"+user.getUsername());
        System.out.println("name:"+user.getPassword());
    }
}
?
?2. [代碼]confirm方法對應的html頁面如下     
<%@ page language="java" contentType="text/html; charset=utf8" pageEncoding="utf8"%>
<!DOCTYPE html>http://www.5idesk.com/s/lol/?
<html>
<head>
<meta charset="utf8"/>
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/index.css" media="screen" />
</head>
<body>
    <form action=‘user/confirm/lw‘ method="post">
        名字:<input name="user.username" type="text" value=""/><br/>
        密碼:<input name="user.password" type="text" value=""/><br/>
        電話:<input name="phone" type="text" value="梁威的電話"/><br/>
         
        愛好:<br/>
        讀書<input name="hobby" type="checkbox" value="讀書"/>
        音樂<input name="hobby" type="checkbox" value="音樂"/>
        遊玩<input name="hobby" type="checkbox" value="遊玩"/>
        夏天<input name="hobby" type="checkbox" value="夏天"/>
         
        <button type="submit">登陸</button>
    </form>
</body>
</html>
?

我的java mvc

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.