MVC框架发展进程
StrutsWebworkStruts2框架 不是Struts的版本升级,反而是以webwork为基础发展的框架SpringMVC 属于Spring框架中的一个模块
MVC框架重要的组件
凡是MVC框架,重点提供的是控制器的解决方案。struts2提供的控制器: 1、中央控制器(Filter) 框架提供,只有一个,程序员不需要实现,只需要统一入口即可 2、Action 由程序员实现功能,有n个struts2的运作流程
strtus2的入门程序
struts2的基本类库添加struts.xml核心配置文件,主要用于配置action在web.xml中配置中央控制器
<filter>
<filter-name>struts2
</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2
</filter-name>
<url-pattern>*.action
</url-pattern>
</filter-mapping>
实现action – javabean的方式 – 实现 Action接口 –继承 ActionSupport类(常用)
public class HelloAction extends ActionSupport {
private String message
;
@Override
public String
execute() throws Exception
{
setMessage("Hello world!");
return "success";
}
public String
getMessage() {
return message
;
}
public void setMessage(String message
) {
this.message
= message
;
}
}
struts.xml的配置
<struts>
<package name="my_pack" extends="struts-default">
<action name="hello" class="com.gec.web.action.HelloAction">
<result name="success">/index.jsp
</result>
</action>
</package>
</struts>
视图层JSP
<body>
<h2>${message }
</h2> <br>
</body>
action类中请求方法的定义
如果在配置文件中没有指定具体的action的方法的,默认要执行execute方法。 同一个Action类中,定义多个请求方法,最常见的配置如下:
<package name="my_pack" extends="struts-default">
<action name="hello" class="com.gec.web.action.HelloAction">
<result name="success">/index.jsp
</result>
</action>
<action name="addAction" class="com.gec.web.action.HelloAction" method="add">
<result name="success">/index.jsp
</result>
</action>
<action name="updateAction" class="com.gec.web.action.HelloAction" method="update">
<result name="success">/index.jsp
</result>
</action>
<action name="deleteAction" class="com.gec.web.action.HelloAction" method="delete">
<result name="success">/index.jsp
</result>
</action>
</package>
Struts2请求参数的接收方式
简单数据类型的接收方式:java基本数据类型和String自定义的类型 ,以实体类的方式来接收
jsp提交表单
<form action="doRegister.action" method="post">
<p>
用户名:
<input type="text" name="user.username"/>
</p>
<p>
密 码:
<input type="password" name="user.password"/>
</p>
<p>
<input type="submit" value="登 录"/>
</p>
</form>
action类的定义
public class UserAction extends ActionSupport {
private User user
;
public String
doRegister() {
System
.out
.println("用户名:" + user
.getUsername());
System
.out
.println("密 码:" + user
.getPassword());
setMessage("欢迎你," + user
.getUsername());
return SUCCESS
;
}
}
获取ServletAPI的方式
解耦的方式
public String
doLogin() {
System
.out
.println(username
);
System
.out
.println(password
);
if ("zhangsan".equals(username
) && "110".equals(password
)) {
} else {
return ERROR
;
}
}
耦合的方式
public String
doLogin() {
System
.out
.println(username
);
System
.out
.println(password
);
if ("zhangsan".equals(username
) && "110".equals(password
)) {
HttpServletRequest request
= ServletActionContext
.getRequest();
ServletContext application
= ServletActionContext
.getServletContext();
HttpSession session
= request
.getSession();
session
.setAttribute("admin", username
);
return SUCCESS
;
} else {
return ERROR
;
}
}
Struts2的文件上传
public class FileAction extends ActionSupport {
private File pic
;
private String picFileName
;
private String picContentType
;
public String
uploadFile() {
try {
String path
= ServletActionContext
.getServletContext().getRealPath("upload");
System
.out
.println(path
);
File targetFile
= new File(path
, picFileName
);
FileUtils
.copyFile(pic
, targetFile
);
System
.out
.println("文件上传成功");
} catch (IOException e
) {
e
.printStackTrace();
}
return SUCCESS
;
}
public File
getPic() {
return pic
;
}
public void setPic(File pic
) {
this.pic
= pic
;
}
public String
getPicFileName() {
return picFileName
;
}
public void setPicFileName(String picFileName
) {
this.picFileName
= picFileName
;
}
public String
getPicContentType() {
return picContentType
;
}
public void setPicContentType(String picContentType
) {
this.picContentType
= picContentType
;
}
}