J2EE dynamic web工程搭建 struts2
来源:互联网 发布:电信4g首选网络类型 编辑:程序博客网 时间:2024/06/13 01:48
工程目录:
搭建struts2关键的jar一共有9个,分别是:
LoginAction.java 类:
package com.cy.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{private static final long serialVersionUID = 1L; private String userName; private String password; @Override public String execute() { if (userName.equals("hellokitty") && password.equals("123")) { return SUCCESS; } else { return ERROR; } } 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; }}web.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>StrutsDemo</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> <!-- struts2.1.3之后的版本,可以在该过滤器之前之间定义一定的过滤器--> <!-- 定义struts2 的核心控制器,用于生成ActionMapper ,拦截所有的Action请求--> <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>struts.xml 这个xml一定要放置在src目录下面:
<?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="default" namespace="/" extends="struts-default"> <action name="login" class="com.cy.action.LoginAction"> <result name="success">/jsp/success.jsp</result> <result name="error">/jsp/error.jsp</result> </action> </package> </struts>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="get"> 用户名:<input type="text" name="userName"><br/> 密 码:<input type="password" name="password"/><br/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form> </body></html>
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=ISO-8859-1"><title>Insert title here</title></head><body> <h1>success!!!!!!!!!</h1></body></html>
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=ISO-8859-1"><title>Insert title here</title></head><body><h1>error!!!!!!!!!</h1></body></html>
默认页面index.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> struts2.</body></html>
在调试的过程中,出现的错误:
type Status report
message There is no Action mapped for namespace [/] and action name [login] associated with context path [/structDemo].
description The requested resource is not available.
1.表单提交的login.action 不是login
<form action="login.action" method="get"> 用户名:<input type="text" name="userName"><br/> 密 码:<input type="password" name="password"/><br/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form>
2.namespace需要添加
<package name="default" namespace="/" extends="struts-default">
3.Java action类 execute方法外面添加 @Override 注解
@Override public String execute() {
0 0
- J2EE dynamic web工程搭建 struts2
- J2EE搭建Dynamic web SpringMVC工程404错误分析(一)
- J2EE搭建Dynamic web SpringMVC工程404错误分析(二)
- J2EE搭建Dynamic web SpringMVC工程404错误分析(三)
- J2EE搭建之七 创建一个Dynamic Web Project
- 导入Dynamic Web Pronject 工程
- 搭建Dynamic Web Project(动态web项目)的springmvc工程1
- 搭建Dynamic Web Project(动态web项目)的springmvc工程2
- 搭建一个简单的struts2.0的web工程
- 搭建一个Struts2工程
- Struts2工程搭建
- eclipse Dynamic Web Project工程报红叉
- j2ee建立的Dynamic Web Project没有web.xml
- j2ee建立的Dynamic Web Project没有web.xml
- Eclipse搭建J2EE WEB环境
- web工程搭建
- Maven搭建Web工程
- maven搭建web工程
- 1.2汇编语言的产生
- JavaScript扫雷游戏mineClearance
- 为开发AR眼镜 苹果挖走NASA技术专家
- urllib2 爬取网页信息
- 1.3汇编语言的组成
- J2EE dynamic web工程搭建 struts2
- HDOJ--2023 求平均成绩
- Spring的缓存注解
- 大数据文本分类、聚类思想
- 1.4存储器
- 汽车VIN码识别做到1秒识别与解析
- 1.5指令和数据
- 补二
- Git命令学习整理