1.struts框架
Struts是最早的Java开源框架之一,它是MVC设计模式的一个优秀实现。 Struts定义了通用的Controller(控制器),通过配置文件(通常是 Struts -config.xml)
Struts是最早的java开源框架之一,它是MVC设计模式的一个优秀实现。 Struts定义了通用的Controller(控制器),通过配置文件(通常是 Struts -config.xml)隔离Model(模型)和View(视图),以Action的概念以对用户请求进行了封装,使代码更加清晰易读。 Struts还提供了自动将请求的数据填充到对象中以及页面标签等简化编码的工具。 Struts能够开发大型Java Web项目。——来自于百度百科
2.sturts2框架的作用
Struts是基于JavaEE应用的MVC设计模式的应用框架,减弱了业务逻辑接口和数据接口之间的耦合,以及让视图层更富于变化。
原来的mvc控制器,不论从设计规、编码的扩展性和维护性都不足
Struts2专门用来管理用户请求
struts2与servlet的比较
servlet是mvc的基础,struts2是从servlet的基础之上发展而来的,由于servlet中需要在web.xml
中配置的文件比较多,从而导致了struts2的发展。
- servlet的请求方式单一,包含get,post,(put,get,post,delete,patch)
- servlet处理请求的能力差,基本上一个请求对应一个servlet,加大了工程目录结构的复杂
- servlet页面内容展示极差,将最底层的api展示给程序员,需要操作的步骤较多,不够智能
- struts2提供了丰富的拦截器的实现类,有着较好的异常处理机制,只需要在配置文件中配置即可
- struts2提供了自带的标签库,给程序员带来了极大的方便
- struts2可以通过配置struts.xml文件来实现对请求流转的控制
- struts2中传值的方式变化了,可以通过属性驱动(需要给getter,setter),对象驱动,模型驱动(实现一个接口类)--具体可自行百度
- struts2可拓展性高
- sturts2安全性不好,最近几年一直爆出高危漏洞
如何使用struts2
1.我是使用的是maven项目,直接在pom.xml配置中导入jar包
<--!sturts2 核心包,其中还有相关的依赖包-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.28</version>
</dependency>
<---sturts2异步请求包-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.28</version>
</dependency>
<---jstl和el表达式包-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
2.在web.xml配置文件--启用struts默认拦截器
<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>/*</url-pattern>
</filter-mapping>
3.配置struts.xml文件(默认凡在src下)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 认识系统中的常量 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.action.extension" value="action,do,song"></constant>
<constant name="struts.devMode" value="true"></constant>
</struts>
4.创建对应的action类(struts2的类包名后缀一定要是,action,sturts2默认调用的是execute方法)
package com.it.action;
import java.util.List;
import java.util.Map;
import com.it.bean.StuInfo;
import com.it.dao.StuInfoDao;
import com.it.dao.StudentDao;
import com.opensymphony.xwork2.ActionContext;
public class StudentAction {
private String stu_id;
private String stu_name;
private StuInfo stu=new StuInfo();
private List<StuInfo> list;
private String username;
private String currentPage;
private String pageSize;
private int currentP;
private int currentS;
private int counts=0;
private int flag=0;
//private int counts=new StudentDao().count();
public String aaa(){
System.out.println("===hello world===");
System.out.println(stu_id+"======"+stu_name);
System.out.println("===1234123===="+stu.getStu_id());
return "success";
}
public String show() throws Exception{
//list=new StudentDao().findAll();
list=new StuInfoDao().findAll_proc();
System.out.println("数据量"+list.size());
//通过actionContext创建request,session
return "success";
}
public String deleteById() throws Exception{
//new StudentDao().deleteById(stu_id);
new StuInfoDao().delete_proc(stu_id);
return "success";
}
public String splitPage() throws Exception{
//counts=new StuInfoDao().findAll_proc().size();
counts=new StuInfoDao().counts_proc15();
/*if(currentPage==null || pageSize==null){
currentP=1;
currentS=1;
}else{
if(Integer.parseInt(currentPage)>=counts){
currentP=counts;
}else if(Integer.parseInt(currentPage)<=0){
currentP=1;
}else{
currentP=Integer.parseInt(currentPage);
}
}
list=new StudentDao().splitPage(currentP, currentS);*/
if(flag==0){
currentP=1;
currentS=5;
}else if(flag==1){
if(currentP>=5){
int temp=currentS;
currentS=currentP;
currentP=2*currentP-temp;
}
}else if(flag==2){
if(currentS<=counts){
int temp=currentP;
currentP=currentS;
currentS=2*currentS-temp;
}
}
list=new StuInfoDao().splitPage_proc(currentP, currentS);
System.out.println(currentP+"==========="+currentS+"----------"+flag);
return "success";
}
public String getStu_id() {
return stu_id;
}
public void setStu_id(String stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public StudentAction(String stu_id, String stu_name) {
super();
this.stu_id = stu_id;
this.stu_name = stu_name;
}
public StudentAction() {
super();
// TODO Auto-generated constructor stub
}
public StuInfo getStu() {
return stu;
}
public void setStu(StuInfo stu) {
this.stu = stu;
}
public List<StuInfo> getList() {
return list;
}
public void setList(List<StuInfo> list) {
this.list = list;
}
public String getCurrentPage() {
return currentPage;
}
public void setCurrentPage(String currentPage) {
this.currentPage = currentPage;
}
public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public int getCurrentP() {
return currentP;
}
public void setCurrentP(int currentP) {
this.currentP = currentP;
}
public int getCurrentS() {
return currentS;
}
public void setCurrentS(int currentS) {
this.currentS = currentS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
5.在struts.xml文件中配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 认识系统中的常量 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.action.extension" value="action,do,song"></constant>
<constant name="struts.devMode" value="true"></constant>
<package name="stu" namespace="/stu" extends="struts-default">
<!--请求需要对应的action的类 -->
<action name="add" class="com.it.action.StudentAction" method="aaa">
<result name="success" type="redirectAction">show</result>
<result name="error">/error.jsp</result>
</action>
<action name="show" class="com.it.action.StudentAction" method="show">
<result name="success">/show.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="splitPage" class="com.it.action.StudentAction" method="splitPage">
<result name="success" >/show.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="deleteById" class="com.it.action.StudentAction" method="deleteById">
<result name="success" type="redirectAction">splitPage</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
补充
如果需要动态方法的调用,则需要配置struts.xml文件
<constant name="struts.enable.DynamicMethodInvocation" value="true" />默认也是true
此时在action中可以进行动态的调用方法了
<action name="teaAction_*" class="com.it.action.TeacherAction" method="{1}">
<result name="success" type="redirectAction">tea/teaAction_show.action</result>
<result name="error">/error.jsp</result>
<result name="lgoin">/login.jsp</result>
<result name="index">/index.jsp</result>
</action>
method中的{1}代表的及时action name的*,这样便实现了方法的动态调用
6.此时补上所缺的界面,运行tomcat服务器,完美!!!