Struts2的介绍、配置、入门

注:需要源码资源私信

Struts的介绍

1、首先Struts2是Struts和webwork的综合

2、它是一个框架,所谓框架就是一些开发者提供的开发工程的一些基本结构,类似于房子中的毛坯房,有待开发者自己进一步实现功能(装修房子)

3、它的本质技术是Servlet和JSP,避免直接使用Servlet和JSP的代码书写不规范、乱用session,维护性差的问题,由于项目最大一部分问题是后期维护,所以Struts这种便于后期维护项目的技术得到推广

设计模式和框架的区别

设计模式:是项目的设计思想,项目的完成都以设计思想为中心,类似于建房子的设计图,有了设计图之后就是按部就班

框架:是项目的一种结构,类似于已经依照设计图完成的房子的骨架,剩下的装修等内容需要开发者自己实现

Struts2的配置步骤

注:所有框架或者技术的使用无非是引入JAR包

1、新建DynamicProject

2、在WEB-INF下的lib中导入一下JAR包

3、配置WEB-INF下的web.xml,将所有请求交给Struts过滤器,进行递交请求给struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HelloStruts</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.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>Struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

4、在src目录下建立struts.xml文件

注意:在这里需要使用标签<struts>,需要导入一段代码

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

5、在WEB-INF下建立客户端请求网页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>
	<form action="user_login">
		用户名:<input type="text" name="userName"/>
		密码:<input type = "password" name= "password"/>
		<input type="submit" value = "登录"/>
	</form>
</body>
</html>

6、在struts.xml文件中配置来自JSP文件的请求

<?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="login" namespace="/" extends="struts-default">
        		 <action name="login" class="com.lanou.user.action.LoginAction" method="login">
        			<result name="success">loginSuccess.jsp</result>
        			<result name="error">loginError.jsp</result>
        		</action> 
        		<!-- 动态方法调用 主要是实现了同一个action处理不同请求 -->
        		<!-- <action name="user" class="com.lanou.user.action.UserAction"></action> -->
        	</package>
        </struts>

7、建立类文件,执行请求

package com.lanou.user.action;

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 String login() {
		if (userName.equals("wangning")&&password.equals("123")) {
			return "success";
		}
		return "error";
	}
}

动态请求的两种方式

注意:类文件中不可以有set 和 get 方法

1、通配符方式

struts.xml文件

        		<action name="user_*" class="com.lanou.user.action.UserAction" method="{1}">
        			<result name="success">{1}Success.jsp</result>
        			<!-- 要使用的方法要在这里声明否则无法调用 -->
        			<allowed-methods>login,register,test</allowed-methods>
        		</action>

2、感叹号方式

struts.xml文件

        		<!-- 第二种动态请求的方式 首先设置允许的方法有哪些 前台在请求时候,action后面加上!加上方法名例如 stu!add-->
        		<action name="stu" class="com.lanou.user.action.StuAction">
        			<result name="success">testSuccess.jsp</result>
        			<allowed-methods>add,delete</allowed-methods>
        		</action>

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>
	<form action="stu!add">
		用户名:<input type="text" name="userName"/>
		密码:<input type = "password" name= "password"/>
		<input type="submit" value = "登录"/>
	</form>
</body>
</html>

strtus.xml文件中action中的的特殊属性

1、name = "input"

        			<result name="input">inputError.jsp</result>

2、type = "dispatcher"

        			<result name="success" type="">webResource.jsp</result>
type默认是dispatcher 

redirect重定向到web资源
redirectAction重定向到另一个action,此时如果不存在这样的action就会404
stream给客户端返回流用于给客户端返回流 用于文件的上传下载

JSON给客户端返回JSON字符串  
input当输入参数验证失败或转换失败时去到的页面




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值