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
原创粉丝点击