第一次摸struts,纠结了好长时间……
我用的是struts2.3.1.2
1.将struts2所需要的类库添加到lib
2.在创建struts2 的配置文件 struts.xml,内容如下:
<?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>
</struts>
<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>
运行一下项目,没有错误,这样,整个环境就搭配好了
示例:
struts.xml如下
<struts>
<package name="test" namespace="/test" extends="struts-default"><!-- 设置路径 -->
<action name="hello" class="cn.ljf.StrutsTest" method="execute"><!--设置所要执行的类cn.ljf.StrutsTest及函数execute -->
<result name="success">/WEB-INF/page/Hello.jsp</result><!-- 设置执行的页面 -->
</action>
</package>
</struts>
如果没有为action指定class,默认是ActionSupport。
没有为action指定method,默认执行action中的execute()方法
没有指定result 的name属性,默认值为success.
package cn.ljf;
public class StrutsTest {
private String str;
public String getStr() {
return str;
}
public String execute() {//返回的类型是String 属性是public
str="hello struts!!!";
return "success";
}
}
Hello.jsp如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%--
@author:ljf
@blog:blog.csdn.net/ljfbest
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ss</title>
</head>
<body>
${str} <!-- struts会自动执行将变量str加入到request域中 -->
</body>
</html>
输入:http://localhost:8080/struts/test/hello
显示 :hello struts!!!
另外:Struts2配置文件无提示问题
找到Struts2 发行包中的 struts-2.0.dtd 文件 ,MyEclipse 的Window--Preferences--MyEclipse—-Files and Editors—XML—XML Catalog Add 按钮,添加一个 Add XML Catalog Entry ,里面的有三个参数分别是:
Location : File System 找到刚刚找到的struts-2.0.dtd 文件。
Key Type : 选 URI
Key :http://struts.apache.org/dtds/struts-2.0.dtd
2.为应用指定多个配置文件
在实际应用中 struts.xml文件中一般只是配置一些全局文件需要的东西,比如一些常量,一般会分模块的配置文件 然后在struts.xml文件中使用include标签就可以完成将这个配置文件移入里面去的功能
<?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="bird" namespace="/test" extends="struts-default">
<action name="helloworld_*" class="com.bird.action.HelloWorld" method="{1}">
<result name="success">/WEB-INF/jsp/hello.jsp</result>
</action>
</package>
</struts>
<?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="bird" namespace="/test" extends="struts-default">
<action name="helloworld_*" class="com.bird.action.HelloWorld" method="{1}">
<result name="success">/WEB-INF/jsp/hello.jsp</result>
</action>
</package>
</struts>
<?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>
<constant name="struts.action.extension" value="do,action"></constant>
<include file="employee.xml"></include>
<include file="user.xml"></include>
</struts>
常用的配置常量介绍
<!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的输出 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="do"/>
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!-– 与spring集成时,指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" />
<!-–该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<!--上传文件的大小限制(总大小)-->
<constant name="struts.multipart.maxSize" value=“10701096"/>
常量可以在struts.xml或struts.properties中配置,建议在struts.xml中配置,两种配置方式如下:
在struts.xml文件中配置常量
<struts>
<constant name="struts.action.extension" value="do"/>
</struts>
在struts.properties中配置常量
struts.action.extension=do
因为常量可以在下面多个配置文件中进行定义,所以我们需要了解struts2加载常量的搜索顺序:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.