一,struts2是什么?

  struts2是一个控制框架,相当于连接底层和显示层,控制页面和数据展示

二,为什么用struts2?

  jsp+javabean模式:jsp里的小脚本java代码太多,页面杂乱  

  jsp+servlet+javabean模式:servlet和jspAPI强耦合,代码复用率低

  jsp+struts2+javabean模式: 把servlet进行了封装,使用拦截器拦截请求,使用结果视图进行响应

三,struts2怎么用?

  1.导入框架所需的包,软件默认在web-info下添加全局过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <!-- 首页配置 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 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> </web-app>

  2.编写action类,【继承ActionSupport】

package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext; import com.bean.User;
import com.dao.IUserDao;
import com.dao.impl.UserDaoImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven<User>{ //文件上传
private File file;
private String fileContentType;
private String fileFileName; public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} //数据接收
private User u; public User getU() {
return u;
}
public void setU(User u) {
this.u = u;
}
@Override
public User getModel() {
if(this.u==null){
this.u=new User();
}
return this.u;
} public String add(){ //保存文件
String path = ServletActionContext.getServletContext().getRealPath("/upload/");
File dir=new File(path);
if(!dir.exists()){
dir.mkdirs();
}
try {
FileInputStream input=new FileInputStream(file);
FileOutputStream output=new FileOutputStream(path+"/"+fileFileName);
IOUtils.copy(input, output);
input.close();
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //调用数据接口,添加数据库
IUserDao iud = new UserDaoImpl();
this.u.setPhoto(fileFileName);
Integer result = iud.addUser(u);
if(result>-1){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
} return SUCCESS;
} }

  3.在struts.xml中注册action和相应的结果视图

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 设置上传文件大小 -->
<constant name="struts.multipart.maxSize" value="10000000"></constant> <package name="test" namespace="/" extends="struts-default"> <action name="*_*" class="com.action.{1}Action" method="{2}">
<!-- 拦截器指定文件大小 -->
<interceptor-ref name="fileUpload">
<param name="maximumSize">10000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/> <result name="success">success.jsp</result>
</action> <!-- 文件上传测试 -->
<action name="Uplod" class="com.action.UploadAction">
<!-- 拦截器指定文件大小 -->
<interceptor-ref name="fileUpload">
<param name="maximumSize">10000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/> <result name="success">success.jsp</result>
</action> </package> </struts>

struts常用知识的更多相关文章

  1. 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇一:WPF常用知识以及本项目设计总结

    篇一:WPF常用知识以及本项目设计总结:http://www.cnblogs.com/baiboy/p/wpf.html 篇二:基于OneNote难点突破和批量识别:http://www.cnblog ...

  2. javascript常用知识点集

    javascript常用知识点集 目录结构 一.jquery源码中常见知识点 二.javascript中原型链常见的知识点 三.常用的方法集知识点 一.jquery源码中常见的知识点 1.string ...

  3. AngularJS进阶(十二)AngularJS常用知识汇总(不断更新中....)

    AngularJS常用知识汇总(不断更新中....) 注:请点击此处进行充电! app.controller('editCtrl',['$http','$location','$rootScope', ...

  4. 打造自己的Android常用知识体系

    前言 Android常用知识体系是什么鬼?所谓常用知识体系,就是指对项目中重复使用率较高的功能点进行梳理.注意哦,不是Android知识体系. 古语道:学而不思则罔,思而不学则殆.如果将做项目类比为“ ...

  5. jQuery常用知识总结

    jQuery常用知识总结 简介 选择器 属性操作 jQuery() each event事件 jQuery扩展 一.简介 What is jQuery jQuery is fast small and ...

  6. Python数据分析与挖掘所需的Pandas常用知识

    Python数据分析与挖掘所需的Pandas常用知识 前言Pandas基于两种数据类型:series与dataframe.一个series是一个一维的数据类型,其中每一个元素都有一个标签.series ...

  7. Oracle常用知识小总结

    永不放弃,一切皆有可能!!! 只为成功找方法,不为失败找借口! Oracle常用知识小总结 1. 创建自增主键 对于习惯了SQL SERVER的图形化界面操作的SQLer,很长一段时间不用oracle ...

  8. php常用知识集锦

    php常用知识集锦 很多位置都有写好的代码,自己做项目的时候可以直接拿来用,而不用自己写,比如现在看到的菜鸟教程. 1.判断是否为空 empty($_POST["name"]) 2 ...

  9. javascript常用知识汇总

    javascript这个语言庞大而复杂,我用了三年多了,还是皮毛都不会.从刚开始的jquery,到后来的es6,每天都在学习,每天都在忘记. 1.禁止手机虚拟键盘弹出 在开发适配手机的页面时,出现了这 ...

随机推荐

  1. IOS XMPP总结

    //前言:仿weixin实现一个即时通讯的案例,支持版本7.0以上 "准备工作 创建项目时使用git" /* 显示隐藏文件,看到git的文件夹 defaults write com ...

  2. Linux中配置ftp服务器

    1. 先用rpm -qa| grep vsftpd命令检查是否已经安装,如果ftp没有安装,使用yum  -y  install vsftpd 安装,(ubuntu 下使用apt-get instal ...

  3. angularJs的指令系统和双向数据绑定

    一.langularJs的指令系统 <!DOCTYPE HTML> <html ng-app><!--这种以ng开头的就是指令系统,初始化的一个指令,不仅可以加在html ...

  4. js 调用 oc 的解释

    JavaScriptCore NSInvocation js解释器在解释函数调用时,会在执行环境进行函数搜索,主调者类型判定: 如果是js调用,直接解释执行: 如果是oc调用,则将调用打包成NSInv ...

  5. 【[NOI2010]超级钢琴】

    我竟然又在写主席树 现在可是九月啦,我却还在写这种noip不可能考的算法 我觉得我真的要凉 题意很明确,就是给你一个序列,让从中选择\(k\)段连续的序列,长度必须大于等于\(L\)小于等于\(R\) ...

  6. jquery全选 反选

    //全选 反选 $('#chkAll').on('click',function(){ $('input.chkbox').prop('checked',$(this).prop('checked') ...

  7. Tomcat中Pipeline

    Pipeline 节选部分源码.源码版本 Tomcat8.5 处理模式 Pipeline--Valve是一种责任链模式,它和普通责任链模式有两点区别: 每个Pipeline都是有特定的Valve,而且 ...

  8. PAT——1041. 考试座位号

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

  9. UVALive - 2515 (最小生成树 kruskal)

    You are assigned to design network connections between certain points in a wide area. You are given ...

  10. Python 学习笔记(十五)Python类拓展(二)方法

    方法 绑定方法和非绑定方法 绑定方法和非绑定方法在创建时没有任何区别,同一方法,既可以为绑定方法,也可以为非绑定方法,一切不同都只在调用时的手法上有所区别. 绑定方法即该方法绑定类的一个实例上,必须将 ...