struts2 零配置

一、新建一个web项目,命名为:struts2

 

struts2 零配置

二、导入strut2所需的jar包 所需jar下载:http://pan.baidu.com/s/1dDxP4Z3

struts2 零配置

三、配置struts2的启动文件,在web.xml添加如下内容

    <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>

四、在src包下,新建struts.xml和struts.properties这2个文件

struts2 零配置

在struts.xml添加如下内容

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<!-- 请求参数的编码方式 -->    

    <constant name="struts.i18n.encoding" value="UTF-8"/>    

    <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->    

    <constant name="struts.configuration.xml.reload" value="true"/>    

    <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->    

    <constant name="struts.devMode" value="true"/>      

    <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->    

    <constant name="struts.serve.static.browserCache" value="false" />   

   <!--  含有Action类的路  从action包开始-->

    <constant name="struts.convention.package.locators" value="action" />

    <package name="json" extends="json-default">

        

    

</package>





</struts>  

在struts.properties文件添加如下内容(指定结果页面的路径)

struts.convention.result.path=/

五、现在我们已经完成了strut2的环境配置了,接下来说介绍个使用的demo

demo1

在src下新建一个包命名为:com,在com包下新建一个包命名为:action,在action包下新建一个包命名为:demo

struts2 零配置

在demo 包下新建一个IndexAction.java的类

package com.action.demo;



import org.apache.struts2.convention.annotation.ParentPackage; import com.opensymphony.xwork2.ActionSupport; @ParentPackage(value = "struts-default") public class IndexAction extends ActionSupport { /** * */ private static final long serialVersionUID = -903752277625921821L; private String name; @Override public String execute() { setName("stuts2 零配置的实现"); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

在WebRoot目录下新建一个文件夹命名为:demo

在WebRoot/demo目录新建一个index.jsp 内容如下

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<%

String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:property value="name"/> </body> </html>

启动tomcat,在浏览器输入:http://localhost:8080/struts2/demo/index

struts2 零配置

 

demo2

在src/com/action/demo包下新建一个类IndexTestAction.java

package com.action.demo;



import org.apache.struts2.convention.annotation.Action; import com.opensymphony.xwork2.ActionSupport; @Action public class IndexTestAction extends ActionSupport { /** * */ private static final long serialVersionUID = -903752277625921821L; private String name; @Override public String execute() { setName("stuts2 零配置的实现,路径配置," + "IndexTestAction对应的结果界面:WebRoot/demo/index-text.jsp action去掉," + "中间有大写的转换成小写,加上'-' " + "例如:TestDemoAction 结果页面的是:test-demo.jsp"); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

在WebRoot/demo目录新建一个index-test.jsp 内容如下

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<%

String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:property value="name"/> </body> </html>

 

启动tomcat,在浏览器输入:http://localhost:8080/struts2/demo/index-test

你可能感兴趣的:(struts2)