Eclipse 搭建Struts2
2024-09-05 16:13:16
Eclipse版本 Mars Release (4.5.0)
Struts版本 struts-2.5.20 下载地址:https://struts.apache.org/download.cgi#struts2520
一、创建web项目
命名为MyStruts2
勾选web.xml
二、拷贝struts的jar包
从struts-2.5.20-all\struts-2.5.20\lib拷贝jar文件,复制到WEB-INF\lib文件夹下
然后配置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">
<display-name>MyStruts2</display-name> <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>
<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>
</web-app>
路径为:
然后创建struts.xml, 路径为src/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">
<!-- START SNIPPET: xworkSample -->
<struts>
<!-- 是否开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.example.struts2.LoginAction" method="login">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
三、进行测试
1、创建index.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="login.action" method="post">
用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
2、创建error.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>
Error!
</body>
</html>
3、创建success.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>
Success!
</body>
</html>
4、新建一个Servlet类,用来将输入的用户名和密码进行测试,使用户名如果正确跳转到success页面,否则到error页面(继承ActionSupport与否都可以)
package com.example.struts2; import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext; public class LoginAction extends ActionSupport { HttpServletRequest req = ServletActionContext.getRequest();
String username = req.getParameter("username");
String password = req.getParameter("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("Larry".equals(username)
&& "123456".equals(password)){
return "result";
}else{
return "error";
}
} }
5、测试接口
1) Success
2)Error
参考:
最新文章
- mysql 日期类型比较
- A Complete Guide to the <;Picture>; Element
- 小米2000万买域名mi.com
- JavaScript网站设计实践(六)编写live.html页面 改进表格显示
- javascript 布局 第20节
- #include <;list>;
- java中JSON串转换并获取数据
- 13-7-5 android Tabhost功能实现
- MySQL常用命令总结2
- POJ2352Stars【树状数组】
- HighGUI图形图像界面初步——滑动条的创建和使用
- HashMap,LinkedHashMap,TreeMap对比
- 自动化测试(web测试selenium框架)
- Vue使用过程中常见问题
- arr.sort()
- Node.js 开发指南
- C#深入理解AutoResetEvent和ManualResetEvent
- web.xml之env-entry
- (最大上升子序列)Monkey and Banana -- hdu -- 1069
- MT【83】三个等号
热门文章
- layui.js---layer;;前端预览pdf
- Linux之redis-sentinel
- [daily] 使用thunderbird通过davmail代理访问Microsoft Exchange Service(OWA)
- java请求url可以带参数
- JDK源码那些事儿之SynchronousQueue下篇
- robot framework 笔记(一)
- Fzu-java1
- 2019/10/22 test T1 题解
- 【转发】c#做端口转发程序支持正向连接和反向链接
- vue组件传值的三种方式,文字版解释