第01讲:1 struts2的介绍和helloworld实现

struts2介绍:
主页:http://struts.apache.org/ 在用户请求和模块化处理方面以及页面的展现这块,Struts2 发挥的屌炸天作用; 相对于传统的 Jsp+Servlet 模式,Struts2 更适合企业级团队开发,方便系统的维护; 最新版本:2.3.16

1,新建项目HeadFirstStrust2chapter1,tomcat7,2.5,添加web.xml;导入核心包(共8个,下载地址:链接:https://pan.baidu.com/s/19y5_AwxcePxVyDLMba-YAw 密码:s9qr),
2,在web.xml中添加核心控制器,

<?xml version="1.0" encoding="UTF-8"?>
<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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>HeadFirstStruts2chapter01</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>
3,在src下,添加struts2.xml配置文件,写package标签,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="suibiandingyi" extends="struts-default">
   
</package>
</struts>
4,建包www.cruise.action包,建类:HelloWorldAction,实现Action
package com.cruise.action;

import com.opensymphony.xwork2.Action;

public class HelloWorldAction implements Action{

    @Override
    public String execute() throws Exception {

       System.out.println("struts2的默认的方法执行了");
       return "success";
    }

}
5,在struts2中,继续写标签<action>,
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="suibiandingyi" extends="struts-default">
    <action name="hello" class="com.cruise.action.HelloWorldAction">
       <result name="success">helloworld.jsp</result>
    </action>
</package>

</struts>
6,WebContent下写helloworld.jsp,修改编码格式为UTF-8
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
你好:欢迎来到Cruise技能学习网!
</body>
</html>

猜你喜欢

转载自blog.csdn.net/u010393325/article/details/83747765