初识 Struts2 - 先来生成一个登录页面体验一下

1.首先下载Struts2 必要的包(大家可以到我的云盘直接下载 struts2 必要包 如果失效了,大家也可以自行下载,我会在下面放一张所需包的图片)

这里写图片描述


2.然后在 eclipse 中创建一个 动态web工程

3.然后大家把之前的下载的包全部拷贝到 WEBContent/WEB-INF/lib/ 这个目录下面。(注意:如果你是在我云盘中下载的,其中有两个文件web.xml 和 struts.xml 不用拷贝到 lib目录下面,这两个文件后面会用到

4.然后在 WEB-INF 目录下面生成一个 web.xml 文件(当然你也可以直接将我的文件拷贝到该目录下),如果你是自己生成的,那么你就将下面的代码拷贝到这个文件中



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

当启动一个web项目时,web容器(这里指tomcat)会去读取他的配置文件 web.xml,上面的代码表示可以拦截到所有的 url 请求。

5.在 java Resources/src/ 目录下生成一个 struts.xml文件,文件代码如下:

““

loginForm.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">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" key="user" />
<s:textfield name="password" key="pass" />
<s:submit key="login" value="submit" />
</s:form>
</body>
</html>

不用管 <s:textfield.../>这个标签是什么意思,为什么与 html 标签不同,因为这时 struts2 特有的标签

error.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
wow something wrong
</body>
</html>

success.jsp

““
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1”
pageEncoding=”ISO-8859-1”%>




Insert title here


this is successful page

““

8.在 src 目录下生成一个 han包(这个包名随便,只是我这个例子中使用的是这个包而已,大家可以自行取名,但是相应在 struts 中 action 的class 也要改变),在这个包下面新建一个 LoginAction 类,这个类继承自 ActionSupport,并且重写 execute方法,代码如下;

““
package han;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

//定义封装请求参数的用户名和密码
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 LoginAction() {

}


//定义处理用户请求的 execute方法,这个方法会被程序自动调用
public String execute() throws Exception {
    //当用户名与密码都为 123 的时候,返回成功字符串,否则返回 错误字符串
    if(getUsername().equals("123") && getPassword().equals("123")){
        System.out.println(5656);
        ActionContext.getContext().getSession().put("user", getUsername());
        System.out.println(3344);
        return SUCCESS;
    }
    return ERROR;
}

}
““

9.这时基本上已经完成了,然后右键 loginForm.jsp,运行run as命令,启动服务器,进入 登录页面,如果用户名和密码都输入 123,那么进入 success.jsp 页面,否则进入error.jsp页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本关的任务是让读者初识Python函数,要求定义一个函数来实现华氏-摄氏温度的转换。 要实现华氏-摄氏温度转换,首先我们需要了解华氏温度和摄氏温度之间的数学关系式。关系式为:摄氏温度 = (华氏温度 - 32) / 1.8。 接下来,我们可以定义一个函数,用于实现温度转换。函数的输入参数为华氏温度,返回值为摄氏温度。 下面是一个示例的Python函数代码: def fahrenheit_to_celsius(fahrenheit): celsius = (fahrenheit - 32) / 1.8 # 华氏-摄氏温度转换公式 return celsius 以上代码,函数名为"fahrenheit_to_celsius",参数为"fahrenheit",表示华氏温度。在函数体内,我们通过华氏-摄氏温度转换的公式计算出摄氏温度,并将其赋值给变量"celsius"。最后,通过"return"语句将摄氏温度作为函数的返回值。 通过定义这个函数,我们可以在其他地方调用它来实现华氏温度到摄氏温度的转换。例如,我们可以输入一个华氏温度的值,然后通过调用函数来获取对应的摄氏温度。 例如,调用函数的示例代码如下: fahrenheit = 32 # 华氏温度 celsius = fahrenheit_to_celsius(fahrenheit) # 调用函数,获取摄氏温度 print("华氏温度", fahrenheit, "对应的摄氏温度为:", celsius) 以上代码,我们将华氏温度设定为32°F,然后通过调用函数"fahrenheit_to_celsius"来获取对应的摄氏温度,并将其赋值给变量"celsius"。最后,通过打印输出,我们可以看到华氏温度32°F对应的摄氏温度为0°C。 通过以上的函数定义和调用示例,我们成功实现了使用Python函数来进行华氏-摄氏温度的转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值