Struts2訪問Servlet

知識點:html

servlet是單例的,Action是多例的,一次請求,建立一個Action的實例java

結果頁面分爲全局和局部兩類(局部優先級更高)web

result標籤:
name : 默認succes
type :頁面跳轉類型
  dispatcher 默認值,請求轉發(action轉發jsp)
  redirect 重定向(action重定向jsp)
  chain 轉發(action轉發action)
  redirectAction 重定向(action重定向action)
  stream struts2中提供文件下載的功能apache

 

代碼以下微信

struts.xmlsession

<?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="demo1" extends="struts-default">
        <!--全局結果頁面-->
        <global-results>
            <result name="success">servlet1/demo2.jsp</result>
        </global-results>
        <action name="requestDemo1" class="com.jinke.servlet1.RequestDemo1"/>
        <action name="requestDemo2" class="com.jinke.servlet1.RequestDemo2"/>
        <action name="requestDemo3" class="com.jinke.servlet1.RequestDemo3"/>

        <!--局部結果頁面-->
        <action name="requestDemo1" class="com.jinke.servlet1.RequestDemo1">
            <result name="success">servlet1/demo2.jsp</result>
        </action>
        <action name="requestDemo2" class="com.jinke.servlet1.RequestDemo2">
            <result name="success">servlet1/demo2.jsp</result>
        </action>
        <action name="requestDemo3" class="com.jinke.servlet1.RequestDemo3">
            <result name="success">servlet1/demo2.jsp</result>
        </action>
    </package>
</struts>

web.xmlapp

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
    <welcome-file-list>
        <welcome-file>servlet1/demo1.jsp</welcome-file>
    </welcome-file-list>
    <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>

demo1.jspjsp

<%-- Created by IntelliJ IDEA. User: wanglei Date: 2019/6/18 Time: 11:03 To change this template use File | Settings | File Templates. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Struts2訪問Servlet的API</h1>
<h3>方式一:完成解耦合的方式</h3>
<form action="${pageContext.request.contextPath}/requestDemo1.action" method="post"> 姓名:<input type="text" name="name"><br/> 密碼:<input type="password" name="password"><br/>
    <input type="submit" value="提交"></form>

<h3>方式二:完成原生的方式訪問</h3>
<form action="${pageContext.request.contextPath}/requestDemo2.action" method="post"> 姓名:<input type="text" name="name"><br/> 密碼:<input type="password" name="password"><br/>
    <input type="submit" value="提交"></form>

<h3>方式三:接口注入的方式訪問</h3>
<form action="${pageContext.request.contextPath}/requestDemo3.action" method="post"> 姓名:<input type="text" name="name"><br/> 密碼:<input type="password" name="password"><br/>
    <input type="submit" value="提交"></form>

</body>
</html>

demo2.jspide

<%-- Created by IntelliJ IDEA. User: wanglei Date: 2019/6/18 Time: 11:24 To change this template use File | Settings | File Templates. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>顯示數據</h1> ${reqName}<br/> ${sessName}<br/> ${appName}<br/>
</body>
</html>

action層post

import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.dispatcher.HttpParameters; /** * 訪問Servlet的API方式一:徹底解耦合的方式 */
public class RequestDemo1 extends ActionSupport { @Override public String execute() throws Exception { //接收參數 //利用struts2中的對象ActionContext對象
        ActionContext context = ActionContext.getContext(); //調用ActionContext中方法
        HttpParameters map = context.getParameters(); // System.out.println(map); //二.向域對象中存入數據
        context.put("reqName", map);//至關於request.setAttribute()
        context.getSession().put("sessName", map);//至關於session.setAttribute()
        context.getApplication().put("appName", map);//至關於application.setAttribute()
        return SUCCESS; } }
import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletRequest; import java.util.Map; /** * 訪問Servlet的API方式二:採用原生的方式 */
public class RequestDemo2 extends ActionSupport { @Override public String execute() throws Exception { //1、接收數據 //直接得到request對象,經過ServletActionSupport
        HttpServletRequest request = ServletActionContext.getRequest(); Map<String, String[]> map = request.getParameterMap(); System.out.println(map.toString()); //2、向域對象中保存數據 //向request中保存數據
        request.setAttribute("reqName", map.toString()); //向session中保存數據
        request.getSession().setAttribute("sessName", map.toString()); //向application中保存數據
        ServletActionContext.getServletContext().setAttribute("appName", map.toString()); return SUCCESS; } }
import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.util.ServletContextAware; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.Map; /** * 訪問Servlet的API方式三:接口注入的方式 */
public class RequestDemo3 extends ActionSupport implements ServletRequestAware, ServletContextAware { private HttpServletRequest request; private ServletContext context; @Override public String execute() throws Exception { //1、接收參數 //經過接口注入的方式得到request對象
        Map<String, String[]> map = request.getParameterMap(); for (String key : map.keySet()) { String[] value = map.get(key); System.out.println(key + "   " + Arrays.toString(value)); } //2、向域對象中保存數據 //向request域中保存數據
        request.setAttribute("reqName", map.toString()); //向session中保存數據
        request.getSession().setAttribute("sessName", map.toString()); //向Application中保存數據
        context.setAttribute("appName", map.toString()); return super.execute(); } @Override public void setServletRequest(HttpServletRequest httpServletRequest) { this.request = httpServletRequest; } @Override public void setServletContext(ServletContext servletContext) { this.context = servletContext; } }

結果

  

歡迎關注個人微信公衆號:安卓圈

相關文章
相關標籤/搜索