您的位置:首页 > 编程语言 > Java开发

struts2项目配置文件及处理流程

2016-02-24 19:08 459 查看
步骤:

第一步:在lib文件夹中导入相关jar包

第二步:在src中编写strtus.xml文件

第三部:修改web.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>strtusProject1</display-name>
<filter>
<filter-name>Strtus2Filter</filter-name>
<filter-class>org.apache.strtus2.dispatcher.ng.filter.StrtusPrepareAndExecuteFilters</filter-class>
</filter>
<filter-mapping>
<filter-name>Strtus2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app></span>


Strtus2配置文件(strtus.xml)

<struts></strtus>  根元素

<bean class=""></bean>  用于创建JavaBean实例

<constant name="" value=""></constant> strtus2默认行为标签

<package name=""></package>  包标签,用于区分不同的请求文件

<include file=""></include> 用于引入其他的xml配置文件

<span style="font-size:18px;"><?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>
<!--用于配置web服务的默认编码集,相当于HttpServletRequest.setCharacterEncoding("UTF-8");-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--用于配置请求拦截的类型,默认为action,若配置以后,则可以拦截action和do-->
<constant name="struts.action.extension" value="do"></constant>
<!--设置浏览器是否缓存静态内容,默认为true,在开发阶段最好为false,防止缓存-->
<constant name="struts.serve.static.browserCache" value="true"></constant>
<!--设置xml是否在修改后自动重新加载,默认为false-->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!--在调试时打印更多的调试信息-->
<constant name="struts.custom.properties" value="true"></constant>
<!--struts默认视图主题-->
<constant name="struts.ui.theme" value="simple"></constant>
</struts> </span>

项目实例

bean.LoginAction

<span style="font-size:18px;">package bean;

/**
* Created by lenovo on 2016/2/25.
*/
public class LoginAction {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public LoginAction(String username, String password) {
super();
this.username = username;
this.password = password;
}
public LoginAction() {
super();
}
public String execute(){
if(this.username.equals("admin")&&this.password.equals("admin")){
return "success";
}else{
return "failed";
}
}

}</span>

web.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>strtusProject1</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></span>


strtus.xml

<span style="font-size:18px;"><?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>
<!--用于配置web服务的默认编码集,相当于HttpServletRequest.setCharacterEncoding("UTF-8");-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--用于配置请求拦截的类型,默认为action,若配置以后,则可以拦截action和do-->
<constant name="struts.action.extension" value="action,do"></constant>
<!--设置浏览器是否缓存静态内容,默认为true,在开发阶段最好为false,防止缓存-->
<constant name="struts.serve.static.browserCache" value="true"></constant>
<!--设置xml是否在修改后自动重新加载,默认为false-->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!--在调试时打印更多的调试信息-->
<constant name="struts.custom.properties" value="true"></constant>
<!--struts默认视图主题-->
<constant name="struts.ui.theme" value="simple"></constant>

<!--name包名 extends 继承包-->
<package name="test" namespace="/new" extends="struts-default">
<!--action相当于servlet,其中的name相当于url http://localhost:8080/项目名/new/login.action -->
<action name="login" class="bean.LoginAction">
<result name="success">/success.jsp</result>
<result name="failed">/failed.jsp</result>
</action>
</package>
</struts>    </span>


Strtus2项目请求到响应的完整过程

HTTP请求
web.xml 经过StrutsPrepareAndExecuteFilter过滤器,只拦截*.action的请求
struts.xml 根据请求的url地址与action地址进行匹配,匹配成功进入相应类文件
前置拦截器 执行相关前置拦截器,例如数据封装,上传文件
*.java(.action) 处理请求的数据,执行类似Servlet的操作,接受数据,持久化数据,返回一个字符串标志结果
后置拦截器 执行相关action后置拦截器,例如异常等信息拦截,日志信息处理等
struts.xml  根据类返回的字符串匹配,跳转到相关的结果
af2b
页面
*.jsp 结果页面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java java ee struts2.0