• struts2基础代码实现


    结构图:

    load.jsp

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'load.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   <body>
    25 <h1>Struts2 入门案例</h1>
    26 <!-- 连接中放置要使用的Action -->
    27 <a href="${pageContext.request.contextPath}/StrutsDemo1">访问Struts2的Action. </a>
    28   </body>
    29 </html>
    30  

    web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3 <display-name>struts2</display-name>
     4   
     5   <!-- 配置Struts2的核心过滤器:前端控制器 -->
     6   <filter>
     7       <filter-name>struts2</filter-name>
     8       <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
     9   
    10     <!-- 通过init-param 元素配置 默认编码 -->
    11     <init-param>
    12     <param-name>struts.iln8.encoding</param-name>
    13     <param-value>UTF-8</param-value>
    14     </init-param>
    15  
    16   </filter>
    17   <filter-mapping>
    18       <filter-name>struts2</filter-name>
    19       <url-pattern>/*</url-pattern>
    20   </filter-mapping>
    21   
    22   <welcome-file-list>
    23     <welcome-file>demo1/load.jsp</welcome-file>
    24   </welcome-file-list>
    25 </web-app>

    StrutsDemo1

     1 package cn.itcast.struts2.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class StrutsDemo1 extends ActionSupport {
     6 
     7     @Override
     8     public String execute() throws Exception {
     9         /**
    10          * 提供一个默认的执行方法execute
    11          * 通常实现信息查询来编写是否登录等逻辑返回不同的字符串
    12          */
    13         System.out.println("StrutsDemo1 中的execute执行了");
    14         return "success";
    15     }
    16 
    17 }

    struts.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC 
     3 "-//Apache Software Foundation//DTD Struts Configuration2.0//EN"
     4 "http://struts.apache.org/dtds/struts-2.0.dtd">
     5 <struts>
     6     <!-- 配置一个包package -->
     7     <package name="demo1" extends="struts-default">
     8     <!-- 配置Action -->
     9     <action name="StrutsDemo1" class="cn.itcast.struts2.action.StrutsDemo1">
    10    <!-- 配置结果页面的跳转 在Action 中根据不同的返回值配置不同的跳转页面 -->
    11    <result name="success">/demo1/main.jsp</result>
    12     </action>    
    13     </package>
    14 </struts>

    main.jsp

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'main.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22   </head>
    23       跳转成功!!
    24   <body>
    25   
    26   
    27   </body>
    28 </html>
  • 相关阅读:
    telerik:RadGrid 在表格中编辑更新数据
    给已存在的表添加一个新字段
    Microsoft.Office.Interop.Excel 导出Excel
    反射导出 Excel
    aspx页面中获取当前浏览器url
    图片切换效果
    .net错误处理机制(转)
    javascript:void(0)知多少
    30款jQuery常用网页焦点图banner图片切换 下载 (转)
    Html.RenderPartial与Html.RenderAction区别(转)
  • 原文地址:https://www.cnblogs.com/the-wang/p/7610271.html
Copyright © 2020-2023  润新知