Sturts

1.搭建环境

1.1导入jar包

1.2 配置web.xml(在xml文件里点击右键,选择Open With ==》Xml Editor)

  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>struts</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

1.3在struts.xml里配置包名(配置action)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
	<struts>
	<!-- name: 配置包名 -->
		<package name="MyPackage" namespace="/" extends="struts-default">
			<action name="LoginAction" class="com.yanzi.web.UserAction" method="execute">
				<result name="success">/index.html</result>
				<result name="error">/login.jsp</result>
			</action>
		</package>
	</struts>

1.4 struts和servlet的区别


启动:
servlet:无
struts:配置filter

创建:
servlet:继承HttpServlet,实现doget,dopost,
		添加注解,或者配置web.xml
struts:继承ActionSupport,写一个带有String返回值且抛出一个异常的函数
		配置struts.xml

封装参数:
servlet:导入包BeanUtils,根据name属性自动封装
struts:实现ModelDriven接口,实现getModel方法

转发与重定向:
servlet:
	转发:request.getRequestDispatcher("/login.jsp").forward(request, response);;
	重定向:response.sendRedirect(request.getContextPath()+"/index.html");
struts:
	转发:
		package
			action
				result默认为转发,
	重定向
		package
			aciton
				result中type="redirect"

1.5 一个普通的struts写法(一般三种写法,这种写法最稳妥,功能最多)

package com.yanzi.web;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yanzi.domain.User;
import com.yanzi.service.UserService;

public class UserAction extends ActionSupport implements ModelDriven<User> {
	public User user = new User();
	
	public String execute() throws Exception {		
		
		System.err.println("我已经运行");		
		UserService userService = new UserService();
		boolean success = userService.findUser(user);
		if(success) {
			return "success";
		}else {
			ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!");
			return "error";
		}
	}

	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}	
}

1.6.struts动态调用方法

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
	<struts>
	<!-- 动态方法调用 新增三行-->
	<constant name="struts.devMode" value="true"></constant>
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
	
	<!-- name: 配置包名 -->
		<package name="MyPackage" namespace="/" extends="struts-default">
		<global-allowed-methods>login,register,kill</global-allowed-methods>
		
			<!-- name:决定了action访问的资源名称 servlet:url-pattern 
				class:action的完整类名
				method:指定调用action中的哪个方法来去处理请求
			-->
			<action name="LoginAction_*" class="com.yanzi.web.UserAction" method="{1}">
				<result name="success">/index.html</result>
				<result name="error">/login.jsp</result>
			</action>
		</package>
	</struts>

1.7.1重定向和转发到网页

            <action name="LoginAction_*" class="com.yanzi.web.UserAction" method="{1}">
				<!-- 重定向 -->
				<result name="success" type="redirect">/index.html</result>
				<!-- 默认转发 -->
				<result name="error">/login.jsp</result>
			</action>

1.7.2 重定向和转发到Action

<action name="LoginActionImpl_*" class="com.yanzi.web.ImplAction method="{1}">
    <!-- 转发到LoginActionDefault -->
	<result name="defaultAction" type="chain">LoginActionDefault</result>
	<!-- 重定向到Action(例如LoginAction_*-->
	<result name="toLogin" type="redirectAction">
		<param name="actionName">LoginAction_login</param>
		<param name="username">${username}</param>
		<param name="password">${password}</param>
	</result>
</action>

1.8 获得参数和传递参数

传递参数:actionContext.getContext().getsession();

2.1 ognl代替jstl和el

foreach→<s:iterator value="userList" var="user"></s:iterator>

if else →<s:if test="num%2==0"><s:if><s:else></s:else>

${ username} →<s:property value="ansnum" />

如果是对象等封装类型:  <s:property value="#user.username" />           #号其实代表的是actionContext.getContext()

2.2 拦截器

package com.yanzi.web;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//创建拦截器的第一种方法,推荐用这种
public class MyIntercept extends MethodFilterInterceptor{
	
	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		
		//return "toLogin";	
		return invocation.invoke();
	}
}
package com.yanzi.web;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
//创建拦截器的第二种方法
public class MyIntercept2 implements Interceptor {
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}

2.2.2 配置拦截器(拦截器只能控制访问action,不能控制访问jsp)

2.2.3 拦截器配置放行

猜你喜欢

转载自blog.csdn.net/qq_35669619/article/details/83833043