LearningNotes_struts2.3.16

基础jar包

    commons-fileupload-1.3.jar

    commons-io-2.2.jar

    commons-lang3-3.1.jar

    commons-logging-1.1.3.jar

    freemarker-2.3.19.jar

    javassist-3.11.0.GA.jar

    ognl-3.0.6.jar

    struts2-core-2.3.16.jar

    xwork-core-2.3.16.jar

    jstl-1.2.jar(可不用)

配置web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<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>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

 编写Action

 

 

 

package com.base.test.struts;

public class TestAction {
	private int number1;
	private int number2;
	private int sum;
	
	public String sum(){
		sum = number1 + number2;
		if(sum > 10)
			return "one";
		else
			return "two";
	}
	

	/**get set*/
	
}

 配置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>
	<package name="test" namespace="/test" extends="struts-default">
		<action name="sum" class="com.base.test.struts.TestAction" method="sum">
			<result name="one">/WEB-INF/jsp/test/one.jsp</result>
			<result name="two">/WEB-INF/jsp/test/two.jsp</result>
		</action>
		
		<action name="test*" class="com.base.test.struts.TestWildCardAction" method="{1}">
			<result name="one">/WEB-INF/jsp/test/{1}.jsp</result>
			<result name="two">/WEB-INF/jsp/test/{1}.jsp</result>
		</action>
		
		<action name="to*" class="com.base.test.struts.UIAction" >
			<result name="success">/WEB-INF/jsp/ui/{1}.jsp</result>
		</action>
	</package>
</struts>

 

页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ include  file="/comm/taglib.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
      <head>
          <title>求和</title>
          <script type="text/javascript" src="${base }/js/jquery-1.4.3.js"></script>
      </head>
  
      <body>
           求代数和${number1}
          <br/>
          <s:form action="sum" namespace="/test">                
              <s:textfield name="number1"  />
              <s:textfield name="number2" />        
              <s:submit value="求和" />            
          </s:form>
      </body>
  </html>

 

 

 

你可能感兴趣的:(struts2)