Struts2,大爷你好!第一天

1.向lib文件夹中配置8种struts2的核心jar包。

配置web.xml文件

注意:因为报错我将xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

中的javaee改为j2ee

然后在第二行加入了<!DOCTYPE xml>

解释: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

此行为核心语句

 

<url-pattern>/*</url-pattern>拦截器

 

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

 

下一步配置struts.xml文件

 <action name="hello" class="com.java1234.action.HelloWorldAction">hello作为地址栏可访问的的名字,
class="com.java1234.action.HelloWorldAction"然后访问这个路径下的文件,这个文件返回一个success的值
然后接收,访问helloWorld.jsp文件
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8   <package name="helloWorld" extends="struts-default">
 9       <action name="hello" class="com.java1234.action.HelloWorldAction">
10           <result name="success">helloWorld.jsp</result>
11       </action>
12       
13  
14   </package>
15 
16 </struts>

 

类文件HelloWorldAction.java文件

 1 package com.java1234.action;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 
 5 public class HelloWorldAction implements Action{
 6 
 7     @Override
 8     public String execute() throws Exception {
 9         System.out.println("执行了Action的默认方法");
10         return SUCCESS;
11     }
12 
13 }

 

最后写被执行的helloWorld.jsp文件

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 Struts2大爷你好!
11 </body>
12 </html>

posted @ 2019-03-21 21:36  小白咚  阅读(130)  评论(0编辑  收藏  举报