struts2入門實例

引言:  html

接觸.net有三、4年的時間了,一直想學習java,中間由於種種緣由耽擱下來。本人學習java的目的,一是多條出路,二是和.net平臺互相印證,畢竟只用一門語言,不管是在框架仍是在眼界方面都會有侷限,所以在看過java基本語法後,火燒眉毛的想看看java的SSH框架都是作什麼用的。本文是在網上copy的一個簡單項目,最後經歷千辛萬苦,終於能運行起來了,其中的問題會在博文的最後描述下。首先說明下,本文只是介紹如何搭建struts2,對其內部運行的基本原理,等稍後更深刻的理解後再作補充。java

 

首先列舉目錄結構(深受其害),以及用到的jar包:web

建立項目的各個部分:apache

web.xml:app

<?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" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   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>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <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>
View Code

struts.xml:框架

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
   <package name="com" extends="struts-default">     
      <action name="hello" 
            class="com.yiibai.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>
View Code

index.jsp:yii

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>hello</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
   <form action="hello">
      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="Say Hello"/>
   </form>
</body>
</html>
View Code

HelloWorld.jsp: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">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>helloworld</title>
</head>
<body>
    Hello World, <s:property value="name"/>
</body>
</html>
View Code

HelloWorldAction.javaide

package com.yiibai.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
    private String name;

       @Override
       public String execute() throws Exception {
          return "success";
        //return SUCCESS;
       }
       
       public String getName() {
          return name;
       }

       public void setName(String name) {
          this.name = name;
       }
}
View Code

 

代碼已經所有貼上去了,整個struts能夠說是經過過濾器action,直接轉到相應的界面上。學習

 

出現的經典問題,相信絕大多數的童鞋們都遇到過:

404,做爲過來人確定知道是找到對應的資源,關鍵是應該怎麼放?網上搜索了千百遍,嘗試將web.xml存放的位置作修改,沒有想到解決了,哈哈。困擾了2個星期。

解決:web.xml文件原本在WebContent下面放着,最後轉移到其下面的WEB-INF下面。解決了,具體是什麼緣由,仍是之後理解了,再補充吧。

、、

、、

、、

相關文章
相關標籤/搜索