public void settxtSize(String txtSize) { this.txtSize = txtSize; } public String gettxtSize() { return this.txtSize; } public void settxtFont(String txtFont) { this.txtFont = txtFont; } public String gettxtFont() { return this.txtFont; } public void settxtColor(String txtColor) { this.txtColor = txtColor; } public String gettxtColor() { return this.txtColor; } }
文件pageJavaBean.jsp文件调用定义的JavaBean,通过JavaBean实现页面属性的设置。文件代码如下:
<%@ page language=\<%@ page import=\
8.5 习题
1. 单选题
(1) MVC中的M是指( )。
A. Model B. View C. Controller D. 以上都不对 答案:A
(2) MVC中的V是指( )。
A. Model B. View C. Controller D. 以上都不对 答案:B
(3) MVC中的C是指( )。
A. Model B. View C. Controller D. 以上都不对
答案:C
(4) 有关struts 2的说法不正确的是( )
A. Struts是一个用于简化MVC框架(Framework)开发的Web应用框架 B. 应用Struts 2不需要进行配置 C. Struts 2 含有丰富的标签
D. Struts2采用了WebWork的核心技术 答案:B
2. 上机练习
参考例8.2,用Struts 2编写一个用户登录的简单Web应用程序。 答案:
配置文件web.xml文件代码如下:
loginAction.java文件代码如下: package user;
import com.opensymphony.xwork2.ActionSupport;
public class loginAction extends ActionSupport { private String userName; private String password; public String execute() throws Exception { if (isInvalid(getUserName())) return \ if (isInvalid(getPassword())) return \
return \ } private boolean isInvalid(String value) { return (value == null || value.length() == 0); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
该文件中只要用户名和密码都不为空即可通过验证。 struts.xml文件代码如下:
\ \
用户登录界面文件login.jsp文件代码如下:
<%@ page contentType=\<%@taglib prefix=\