早期流行的JavaWeb框架
配置简介
使用
创建maven web项目
引入struts2依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.22</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.5.22</version> </dependency>
|
web.xml配置struts2前端控制器StrutsPrepareAndExecuteFilter
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
|
添加struts.xml核心配置文件,配置package(extends=”struts-default”),action(name,class,method),result(name,视图路径)
处理流程
- 用户发起请求
- 进入前端过滤器,做请求分发
- 进入action代理对象,在代理对象中,先执行拦截器链,再执行action方法,action方法完成后,继续逆向执行拦截器链
- 得到视图路径
struts.xml配置
基础配置视图解析器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="sys" extends="struts-default" namespace="/sys"> name写指定访问时的URL映射路径,method指的是访问的方法名,class指的是完整的类名 <action name="hello" method="hello" class="io.jtxyh.sys.HelloStruts"> <result name="hello">/index.jsp</result> </action> </package> </struts>
|
1 2 3 4
| public String hello(){ return "hello"; }
|
传指定的数据给前端
1 2 3 4 5 6 7 8
| <package name="sys" extends="struts-default" namespace="/sys"> <action name="userInfo" method="userInfo" class="io.jtxyh.sys.HelloStruts"> <result name="userInfo">/index.jsp</result> </action> <action name="userList" method="userList" class="io.jtxyh.sys.HelloStruts"> <result name="userList">/index.jsp</result> </action> </package>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| private User u; private List<User> userList;
public String userInfo(){ u = new User("张三","20"); return "userInfo"; }
public String userList(){ userList = new ArrayList<>(); userList.add(new User("测试","11")); userList.add(new User("测试2","12")); userList.add(new User("测试3","13")); return "userList"; }
|
1 2 3 4 5 6 7 8 9 10 11
| <!-- 页面可以直接用el表达式获取 --> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>userInfo:${u}</h3> <h3>userList:${userList}</h3> </body> </html>
|
json数据获取
1 2 3 4 5 6 7 8 9 10
| <package name="sys2" extends="json-default" namespace="/sys2"> <action name="getJson" method="getJson" class="io.jtxyh.sys.SysJson"> <result name="ajaxUser" type="json"> <param name="root">user</param> </result> </action> </package>
|
1 2 3 4 5 6 7 8 9
| private User user;
public String getJson(){ user = new User("user","19"); return "getJson"; }
|
action转发和重定向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <package>
<action name="goUserList" method="goUserList" class="io.jtxyh.sys.HelloStruts"> <result name="goUserList" type="chain">userList</result> </action> <action name="goSysJsonUList" method="goSysJsonUList" class="io.jtxyh.sys.HelloStruts"> <result name="goSysJsonUList" type="redirectAction"> <param name="namespace">/sys2</param> <param name="actionName">userList</param> </result> </action> </package>
|
动态调用
1 2 3 4 5 6 7 8 9 10 11 12 13
|
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="sys" extends="json-default" namespace="/sys" strict-method-invocation="false"> <action name="user_*" method="{1}" class="io.jtxyh.sys.HelloStruts"> <result name="hello">/index.jsp</result> <result name="userInfo">/index.jsp</result> <result name="userList">/index.jsp</result> </action> </package>
|
文件上传
- 需要struts2中的commons-fileupload.jar;commons-io.jar包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| private File img;
private String imgFileName;
private String imgContentType;
public String fileupload(){ System.out.println(img.length()); System.out.println(imgFileName); System.out.println(imgContentType); String uploadFilePath = ServletActionContext.getServletContext().getRealPath("uploadFile"); File newImg = new File(uploadFilePath,imgFileName); try { FileUtils.copyFile(img, newImg); } catch (IOException e) { e.printStackTrace(); } return null; }
|
配置拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class MyInterceptor extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation actionInvocation) throws Exception { String invoke = null; System.out.println("这里写的是前置拦截"); if(ServletActionContext.getRequest().getSession().getAttribute("login") != null){ '放行' invoke = actionInvocation.invoke(); System.out.println("这里写的是后置拦截"); }else{ invoke = "gologin"; } return invoke; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <package name="sys" extends="json-default" namespace="/sys" strict-method-invocation="false"> <interceptors> <interceptor name="interecptorTest" class="io.jtxyh.interceptor.MyInterceptor"> <param name="excludeMethods">hello,userList</param> </interceptor>
<interceptor-stack name="myDefaultStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="interecptorTest"/> </interceptor-stack>
</interceptors>
<default-interceptor-ref name="myDefaultStack"/>
<global-results> <result name="gologin" type="redirect">/login.jsp</result> </global-results> </package>
|
相关文章
数据库连接池
SpringIOC
Junit和Spring
Tomcat
Servlet
Request,Response和ServletContext
Cookie和Session
JSP和EL和Jstl
Filter和Listener
Mybatis