我是靠谱客的博主 大胆花生,最近开发中收集的这篇文章主要介绍SSH之Struts入门,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目标:
Struts的初步使用:
1、Struts的配置

          依赖->导入jar包->maven的pom.xml

          做配置->web.xml配置中央控制器->配置过滤器

          框架配置文件->mvc.xml->struts.xml

          开发->写子控制器继承ActionSupport

 

2、Struts动态方法调用

3、Struts的传参

   自定义mvc:Modeldriver接口传参;

   Struts:①、Modeldriver接口传参;

                ②、set方法传参;

                ③、通过对象属性传参;

一、Struts

1、Struts的配置(pom.xml文件)

导jar包:Struts、Tomcat

Struts:

     <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.13</version>
        </dependency>

Tomcat:

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

配置过滤器

路径(org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter)

Ctrl+Shift+T:查找

 在web.xml文件中做配置

<filter>
      <filter-name>struts</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  </filter>
  <filter-mapping>
     <filter-name>struts</filter-name>
     <url-pattern>*.action</url-pattern>
  </filter-mapping>

导入配置文件

 struts.xml

<?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>
<!-- 引入了struts的默认配置 -->
	<include file="struts-default.xml"></include>
	<!-- 开发人员对struts框架的基本配置 -->
	<include file="struts-base.xml"></include>
	<!-- 项目涉及到的模块,分文件管理 -->
	<include file="struts-sy.xml"></include>
</struts>

2、Struts动态方法调用

struts-sy.xml

<?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>
	<package name="sy" extends="base" namespace="/sy">
	<!-- {1}代表第1个* 调后台的哪一个方法-->
	<!-- {2}代表第2个* 调用方法后跳转哪个界面-->
	<action name="/demo1_*" class="com.lsy.one.web.Demo1Action" method="{1}">
		<result name="bookEdit">/bookEdit.jsp</result>
	</action>
	
	<!-- <action name="/demo1_*_*" class="com.lsy.one.web.Demo1Action" method="{1}">
		<result name="{2}">/{2}.jsp</result>
	</action> -->
	
	</package>
</struts>

Demo1Action 
 

package com.lsy.one.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.lsy.one.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Demo1Action extends ActionSupport{

	

	public String add() throws Exception {
		System.out.println("add方法.....");
		return "bookEdit";
	}
	
	public String del() throws Exception {
		System.out.println("del方法.....");
		return "bookEdit";
	}
	
	public String edit() throws Exception {
		System.out.println("del方法.....");
		return "bookEdit";
	}
	
	
	
}

demo1.jsp(操作界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>struts的动态方法调用</h3>
<a href="${pageContext.request.contextPath }/sy/demo1_add.action">新增</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_edit.action">修改</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action">删除 </a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_add_bookEdit.action">删除 </a><br>

</body>
</html>

bookEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功界面
</body>
</html>

3、Struts的传参

建立实体类user

package com.lsy.one.entity;

public class User {
	private String uid;
	private String uname;
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	@Override
	public String toString() {
		return "User [uid=" + uid + ", uname=" + uname + "]";
	}
	
}

三种传参方法:

(1)、Modeldriver接口传参

Demo1Action 

package com.lsy.one.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.lsy.one.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Demo1Action extends ActionSupport{
	private User user1=new User();
	
	
	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}


	
	public String list() throws Exception {
		System.out.println("list方法.....");
		System.out.println(user1);
		System.out.println(sex);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("age", 12);
		req.setAttribute("hobby", "打王者");
		return "bookEdit";
	}
	
	@Override
	public User getModel() {
		return user1;
	}

}

demo1.jsp

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=zs">ModelDriver接口传参</a><br>

 

  (2)、set方法传参

Demo1Action 

package com.lsy.one.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.lsy.one.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

	private User user1=new User();
	private String sex;

	
	
	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}


	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	
	
	public String list() throws Exception {
		System.out.println("list方法.....");
		System.out.println(user1);
		System.out.println(sex);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("age", 12);
		req.setAttribute("hobby", "打王者");
		return "bookEdit";
	}
	
	@Override
	public User getModel() {
		return user1;
	}

	
}

demo1.jsp 

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=nv">set方法传参</a><br>

结果展示

 (3)通过对象属性传参

 Demo1Action 

package com.lsy.one.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.lsy.one.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Demo1Action extends ActionSupport implements ModelDriven<User>{

	private User user1=new User();
	private User user2;
	
	
	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}

	public User getUser2() {
		return user2;
	}

	public void setUser2(User user2) {
		this.user2 = user2;
	}

	
	
	
	public String list() throws Exception {
		System.out.println("list方法.....");
		System.out.println(user1);
		System.out.println(sex);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("age", 12);
		req.setAttribute("hobby", "打王者");
		return "bookEdit";
	}
	
	@Override
	public User getModel() {
		return user1;
	}

}

demo1.jsp 

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&uname=lsy">对象属性传参 </a><br>

4、Struts与Tomcat的集成

两种方式:

①、类置类

HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("age", 12);

 ②、实现接口

实现两个接口:ServletRequestAware、ServletResponseAware

重写方法:

 

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.resp=arg0;
		
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.req=arg0;
	}

 Demo1Action 全界面

package com.lsy.one.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.lsy.one.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{
	private HttpServletRequest req;
	private HttpServletResponse resp;
	
	
	private User user1=new User();
	private String sex;
	private User user2;
	
	
	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}

	public User getUser2() {
		return user2;
	}

	public void setUser2(User user2) {
		this.user2 = user2;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String add() throws Exception {
		System.out.println("add方法.....");
		return "bookEdit";
	}
	
	public String del() throws Exception {
		System.out.println("del方法.....");
		return "bookEdit";
	}
	
	public String edit() throws Exception {
		System.out.println("del方法.....");
		return "bookEdit";
	}
	
	public String list() throws Exception {
		System.out.println("list方法.....");
		System.out.println(user1);
		System.out.println(sex);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("age", 12);
		req.setAttribute("hobby", "打王者");
		return "bookEdit";
	}
	
	@Override
	public User getModel() {
		return user1;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.resp=arg0;
		
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.req=arg0;
	}
}

bookEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功界面
${age }
${hobby }
</body>
</html>

结果展示

再见

 

最后

以上就是大胆花生为你收集整理的SSH之Struts入门的全部内容,希望文章能够帮你解决SSH之Struts入门所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部