J2EE dynamic web工程搭建 struts2
生活随笔
收集整理的這篇文章主要介紹了
J2EE dynamic web工程搭建 struts2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工程目錄:
搭建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;@Overridepublic 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 注解 @Overridepublic String execute() {
總結
以上是生活随笔為你收集整理的J2EE dynamic web工程搭建 struts2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前天涯社区执行总编欲直播带货:筹款300
- 下一篇: J2EE搭建maven工程(选择使用jd