Struts2 入门 hellow word

来源:互联网 发布:怎样手机域名保护 编辑:程序博客网 时间:2024/06/07 00:00

1.添加相应的jar 

  • commons-fileupload-x.y.z.jar

  • commons-io-x.y.z.jar

  • commons-lang-x.y.jar

  • commons-logging-x.y.z.jar

  • commons-logging-api-x.y.jar

  • freemarker-x.y.z.jar

  • javassist-.xy.z.GA

  • ognl-x.y.z.jar

  • struts2-core-x.y.z.jar

  • xwork-core.x.y.z.jar

2.建立项目的第一项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" 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>struts2</display-name><filter><filter-name>struts</filter-name>   //过滤器名称<filter-class>    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>result.jsp</welcome-file></welcome-file-list></web-app>
3.java项目下的.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="default" namespace="/" extends="struts-default">  //都为默认的   namespac="/" 这边为显示的为最后面/ 例如http://localhost:8080/struts2/  <action name="hellowrod"  class="com.action.helloword">  //这边的name 为访问时的名称  <result>/result.jsp</result>// 返回的JSP页面为  <result name="add">/add.jsp</result>  </action></package></struts>
4.类文件编写

package com.action;import com.opensymphony.xwork2.ActionSupport;public class hellowrod  extends ActionSupport{   //继承ActionSupprot类@Overridepublic String execute() throws Exception {System.out.println("hello  wrod");    //测试打印return SUCCESS;      //这边默认反悔SUCCESS  也可以直接放回.XML文件里面定义的name名字}public  String add(){return "add";}public  String updata(){return "updata";}}
5.jsp页面的编写 result.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=UTF-8"><title>Insert title here</title></head><body>   this is  result;</body></html>
测试


通过hellowrod.action 访问

 也可以不用action都可以访问的到


原创粉丝点击