struts

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(sex);
	
		return "bookEdit";
	}

一.struts的配置

1、maven项目搭建好了之后;配置jar包依赖(pom.xml文件中)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zy</groupId>
  <artifactId>maven_2</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven_2 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.1</version>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.5.13</version>
		</dependency>
  </dependencies>
  <build>
    <finalName>maven_2</finalName>
    <plugins>
    
    <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
    
    </plugins>
  </build>
</project>

2、web.xml做配置(配置过滤器)

struts

在web.xml中配置struts环境 

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
 
  <display-name>Archetype Created Web Application</display-name>
<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>
 
</web-app>

 3、struts框架配置文件配置

struts

二.struts的动态方法调用

1、子控制器继承ActionSupport

package com.xly.struts;
 
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.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zy.entity.User;
 
public class Demo1Action extends ActionSupport {
 
	
 
	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("新增");
		return "bookEdit";
	}
	
 
	public String del() throws Exception {
		System.out.println("del");
		return "bookEdit";
	}
 
 
	public String edit() throws Exception {
		System.out.println("edit");
		return "bookEdit";
	}
	public String list() throws Exception {
		System.out.println("list");
		
		return "bookEdit";
	}
 
	
 
 
	
 
 
}

 2、配置子控制器

<?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}代表第一个*  调后台的那个一个方法-->
 <action name="/demo1_*" class="com.zy.struts.Demo1Action" method="{1}">
	<result name="bookEdit">/bookEdit.jsp</result>
	</action> 
	<!--{2}代表第2个*  调后台的那个一个方法,然后跳转到哪一个界面-->
	<!-- <action name="/demo1_*_*" class="com.zy.struts.Demo1Action" method="{1}">
	<result name="{2}">/{2}.jsp</result>
	</action> -->
	
	</package>
	
</struts>

3、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_del.action">修改</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_edit.action">删除</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_add_bookEdit.action">新增a</a><br>
</body>
</html>

4、运行结果

struts

三.struts的传参 

1、Modeldriver接口传参(建实体类)

package com.xly.entity;
 
public class User {
	private String uid;
	private String name;
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public User() {
		// TODO Auto-generated constructor stub
	}
	public User(String uid, String name) {
		super();
		this.uid = uid;
		this.name = name;
	}
	@Override
	public String toString() {
		return "User [uid=" + uid + ", name=" + name + "]";
	}
	
 
}

1.2、子控制器

 
public class Demo1Action extends ActionSupport implements ModelDriven<User>{
	private User user1=new User(); 
	
	public String list() throws Exception {
		System.out.println("list-----");
		System.out.println(user1);
		return "bookEdit";
	}
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user1;
	}
 

1.3、Demo1.jsp界面设置传参

<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=01&&name=hh">Modeldriver传参</a><br> 

struts

 2、set方法传参

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(sex);
	
		return "bookEdit";
	}

 2.1、Demo1.jsp界面设置传参

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

效果展示

struts

3、对象传参 

	private User user2;
	
 
	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(user2);
		
		return "bookEdit";
	}

3.1、demo.jsp界面展示代码

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

struts

 四.struts与tomcat的集成

1、 通过内置类进行集成

struts

 2、第二种方法实现两个接口:ServletRequestAware、ServletResponseAware重新写方法

package com.xly.struts;
 
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.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xly.entity.User;
 
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 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("新增");
		return "bookEdit";
	}
	
 
	public String del() throws Exception {
		System.out.println("del");
		return "bookEdit";
	}
 
 
	public String edit() throws Exception {
		System.out.println("edit");
		return "bookEdit";
	}
	public String list() throws Exception {
		System.out.println("list");
		/*System.out.println(user1);*/
		/*		System.out.println(sex);*/
	System.out.println(user2);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("age", 123);
		req.setAttribute("hobby", "hh");
		return "bookEdit";
	}
 
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user1;
	}
 
 
	@Override
	public void setServletResponse(HttpServletResponse  arg0) {
		this.resp=arg0;
		
	}
 
 
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.req=arg0;
		
	}
 
	
 
 
}

  2.2、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>

效果展示

struts

上一篇:SQL11.11


下一篇:Java封装