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

步骤:

第一步:在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项目请求到响应的完整过程

  1. HTTP请求
  2. web.xml 经过StrutsPrepareAndExecuteFilter过滤器,只拦截*.action的请求
  3. struts.xml 根据请求的url地址与action地址进行匹配,匹配成功进入相应类文件
  4. 前置拦截器 执行相关前置拦截器,例如数据封装,上传文件
  5. *.java(.action) 处理请求的数据,执行类似Servlet的操作,接受数据,持久化数据,返回一个字符串标志结果
  6. 后置拦截器 执行相关action后置拦截器,例如异常等信息拦截,日志信息处理等
  7. struts.xml  根据类返回的字符串匹配,跳转到相关的结果页面
  8. *.jsp 结果页面

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值