框架就是一个模板,保持不变。许多繁琐固定的操作(代码)它帮你做了,我们要做的就是对其添砖加瓦。要使用框架就必须遵守框架的规则。对于web应用,最常用的一个框架就是Struts2。
Struts2帮我们完成:将用户请求映射到一个java类;获取用户提交的数据;将数据返回前端;控制视图跳转等,不再需要自己写servlet来做这些事情。我们只要做一件事,就是编写处理业务的方法。要struts2帮我们做事,那我们就要按它的规则一步步做好配置。
- Struts2项目:模拟登陆
- (1) 创建web project:
- (2) 添加jar包:去官网下https://struts.apache.org/
- (3) 配置web.xml 文件:
- (1) 创建web project:
<?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">
<!--Struts2的核心过滤器,url为*.action表示全部以.action的链接请求都可进入。 -->
<filter>
<!-- name:任意,标识此<filter>-->
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!-- name:指定url映射到的filter-->
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--默认访问页 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
- (4)编写 Action 类:HelloAction.java,有三种方式,这里采用第二种。
import com.opensymphony.xwork2.Action;
public class HelloAction implements Action{
private String name;
private String pwd;
@Override
public String execute() throws Exception {
if("yjqj".equals(name) && "520".equals(pwd)){
return "success";
}else{
return "false";
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
注:在servlet中,默认执行service 方法。在struts2中,默认执行execute方法。在servlet中,service方法有HttpServletRequest和HttpServletResponse参数,无返回值;在struts2中,execute 方法都是public 的,没有输入参数,并有返回值都是String类型。
在Action类中为属性创建set/get方法,struts2会根据set方法会自动注入前端(jsp)表单提交的数据,根据get方法jsp文件会自动获取类中属性值。
Action类中的属性名要和前端(jsp)表单中元素名称要一致;
- (5)创建并完成strust.xml文件配置:
- 必须在src下创建,名字必须为strust.xml。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--name:任意,标识符. extends 必须写,直接或者间接继承struts-default -->
<package name="hello" extends="struts-default">
<!-- name:任意,url请求名称 但是不要写“/” ;class对应action完全限定名 = 包名+类名 -->
<action name="login" class="cn.yjqj.struts2.HelloAction">
<!-- result是结果,name:匹配action的return字串,并映射到指定路由。没写name默认"success" -->
<result name="success"type="dispatcher">/success.jsp</result>
<result name="false" type="dispatcher">/login.jsp</result>
</action>
</package>
</struts>
注: action中的name就是与下面jsp表单中的action对应,但是不需要写后缀.action。
- (6) login.jsp 配置:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login.action" method="post">
用户名:<input type="text" name="name"/><br>
密码:<input type="password" name="pwd"/><br>
<input type="submit" value = "登录">
</form>
</body>
</html>
注:jsp表单中,用户名和密码name值(name和pwd),与action类中属性名保持一致。
jsp表单中,action是提交的地址,. action是扩展名,与web.xml中配置的struts2的核心过滤器相匹配,即filter的<url-pattern>*.action</url-pattern>。
将专案部署到tomcat:
结果:
另:在struts2的URL中," ! "是用来指定执行Action中哪一个方法," ? "后是接参数,参数间连接用" & "。
e.g :http://localhost:8080/AOSapp/BillAction!findBill?username=xxx&passwd=xxx
仅为个人理解,如有不足,请指教。 https://blog.csdn.net/weixin_35811044