struts2(2)---入门示例
来源:互联网 发布:淘宝云客服怎么加入 编辑:程序博客网 时间:2024/06/05 16:19
步骤1:新建web project项目Struts2Demo
步骤2:导入struts2所需要的jar包
步骤3:在src下新建struts2核心配置文件struts.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><package name="loginPackage" namespace="/" extends="struts-default"><action name="loginAction" class="com.cn.action.LoginAction" method="login"><result name="loginStr">/WEB-INF/jsp/user/login.jsp</result></action><action name="toLoginAction" class="com.cn.action.LoginAction" method="toLogin"><result name="loginSucc">/WEB-INF/jsp/user/loginSuccess.jsp</result><result name="loginFail">/WEB-INF/jsp/user/loginFailure.jsp</result></action></package></struts>
步骤4:新建包com.cn.vo,并在该包下新建实体类UserVO
package com.cn.vo;public class UserVO {private int id;private String username;private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}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;}}
步骤5:新建包com.cn.action,并在该包下新建控制器类LoginAction
package com.cn.action;import com.cn.vo.UserVO;public class LoginAction {private UserVO userVO;/** * 登录跳转 * */public String login(){return "loginStr";}/** * 登录处理 * */public String toLogin(){String username=userVO.getUsername()!=null ? userVO.getUsername():"";String password=userVO.getPassword()!=null?userVO.getPassword():"";if("admin".equals(username) && "123456".equals(password)){return "loginSucc";}else{return "loginFail";}}public UserVO getUserVO() {return userVO;}public void setUserVO(UserVO userVO) {this.userVO = userVO;}}
步骤6:在WEB-INF下新建文件夹jsp,然后在jsp文件下新建user文件,然后在user文件下新建三个视图
登录视图: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="toLoginAction.action" method="post"> <table> <tr> <td>用户名:</td> <td> <input type="text" name="userVO.username" /> </td> </tr> <tr> <td>密 码:</td> <td> <input type="password" name="userVO.password" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="登录" /> </td> </tr> </table> </form> </body></html>
登录成功跳转视图:loginSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%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> <span>登录成功,欢迎您${userVO.username}</span> </body></html>
登录失败跳转视图:loginFailure.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> <span>登录失败,错误的用户名${userVO.username}</span><br/> <a href="loginAction.action">返回</a> </body></html>
步骤7:在web.xml中配置struts2的核心过滤器Struts
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置struts2核心过滤器 --> <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>*.action</url-pattern> </filter-mapping></web-app>
步骤8:测试
在浏览器地址栏中输入:http://localhost:8080/Struts2Demo/loginAction.action测试
注:上面的struts2配置为最 基本的配置,实际开发中struts.xml配置用通配符进行配置
阅读全文
0 0
- struts2(2)---入门示例
- Struts2入门(2): 应用简单示例
- Struts2入门示例教程
- Struts2入门示例教程
- Struts2入门示例教程
- Struts2入门示例教程
- Struts2入门示例教程
- Struts2入门示例教程
- Struts2入门示例教程
- Struts2入门示例
- Struts2入门示例教程
- Struts2入门简单示例
- struts2框架入门示例
- Struts2入门示例教程
- struts2简单入门示例
- struts2简单入门示例
- struts2入门示例
- Struts2入门示例教程
- servlet中resquest的简单总结
- JAVA面试笔试中常见的区别
- 使用sap BO sap BO报表制作财务三栏明细账
- angular2环境搭建(Linux和Windows)
- 简单的代码,读懂观察者模式和Promise
- struts2(2)---入门示例
- Linux中的自动安装脚本
- 在vs中char类型的实参与LPCWSTR类型的形参类型不兼容怎么解决
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
- c语言printf();函数输出格式
- 用CSS定位元素
- 网站建立指南
- HttpWebRequest类
- Java 判断两个多边形相交 Polygon intersect Polygon