国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

在Spring中添加Bean

2019-11-18 16:08:18
字體:
供稿:網(wǎng)友

在上次的MVC基礎(chǔ)上,我們準(zhǔn)備給Controller增加Model,為此創(chuàng)建一個(gè)Account Bean Class:

public class Account implements java.io.Serializable {
    PRivate String username;
    private String passWord;

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

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

然后修改SpringappController:

public class SpringappController implements Controller {
    private Account account;
    public Account getUserAccount() { return this.account; }
    public void setUserAccount(Account account) { this.account = account; }

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        return new ModelAndView("view.jsp", "model", account.getUsername());
    }
}

為了向SpringappController注入Bean,我們修改配置文件springapp-servlet.xml

    <!-- 申明一個(gè)Account Bean -->
    <bean id="adminAccount" class="Account">
        <property name="username">
            <value>administrator</value>
        </property>
        <property name="password">
            <value>1234567</value>
        </property>
    </bean>
    <bean id="springappController" class="SpringappController">
        <!-- 注入到Controller -->
        <property name="userAccount">
            <ref bean="adminAccount" />
        </property>
    </bean>

OK,運(yùn)行Ant,然后啟動Tomcat,結(jié)果輸出:


Result: administrator

注入成功!但是在Controller中放一個(gè)Account肯定是不合邏輯的,我們準(zhǔn)備做一個(gè)用戶登錄功能,為此創(chuàng)建一個(gè)AccountManager Bean:

public class AccountManager implements java.io.Serializable {
    Account getAccount(String username, String password) {
        // TODO...
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return account;
    }
}

然后把SpringappController改為:

public class SpringappController implements Controller {
    private AccountManager manager;
    public AccountManager getManager() { return this.manager; }
    public void setManager(AccountManager manager) { this.manager = manager; }

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        return new ModelAndView("view.jsp", "model",
            manager.getAccount(username, password).getUsername());
    }
}

修改springapp-servlet.xml:

    <bean id="accountManager" class="AccountManager" />
    <bean id="springappController" class="SpringappController">
        <property name="manager">
            <ref bean="accountManager" />
        </property>
    </bean>

編譯,運(yùn)行,輸入:http://localhost:8080/springmvc/hello.c?username=xuefeng&password=1234

結(jié)果顯示:

Result: xuefeng

OK,現(xiàn)在只要?jiǎng)?chuàng)建一個(gè)Html form,Post到Controller然后在AccountManager中驗(yàn)證Username和Password就可以了。

(出處:http://www.survivalescaperooms.com)



發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 禹城市| 宁陕县| 嵊泗县| 德昌县| 独山县| 阜阳市| 黄龙县| 沂水县| 盐山县| 临清市| 沈阳市| 门源| 福建省| 肥西县| 大冶市| 随州市| 通化县| 嘉义市| 凤阳县| 灌阳县| 北票市| 开化县| 兴国县| 北碚区| 定州市| 阿尔山市| 略阳县| 吴旗县| 屯昌县| 定日县| 庆阳市| 两当县| 名山县| 万宁市| 阿拉善右旗| 苏州市| 宁津县| 米林县| 米林县| 天门市| 太湖县|