本文主要讲述struts2的基本配置,注意是基本配置,struts2的配置不仅仅是这些。主要是为了演示第一个程序,所以本人认为越简单越好。
大多的配置基本和webwork相同。如果你会webwork只需要你5分钟的时间。
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>strutsfilter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
- filter>
- <filter-mapping>
- <filter-name>strutsfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>
建一个index.jsp页面如下:
<%@ page contentType="text/html; charset=GBK"%><o:p></o:p>
<%@ taglib prefix="s" uri="/struts-tags"%><o:p></o:p>
<html><o:p></o:p>
<head><o:p></o:p>
<title>indextitle><o:p></o:p>
head><o:p></o:p>
<body><o:p></o:p>
<s:form action="index"><o:p></o:p>
<s:textfield name="userName" label="enter your name">s:textfield><o:p></o:p>
<s:submit /><o:p></o:p>
s:form><o:p></o:p>
body><o:p></o:p>
html><o:p></o:p>
建一个IndexAction类代码如下:
- package com.test.struts2;
- import com.opensymphony.xwork2.ActionSupport;
- @SuppressWarnings("serial")
- public class IndexAction extends ActionSupport{
- private String userName;这里的个名字要和表单的名字一样
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String sayHello() throws Exception {
- return SUCCESS;
- }
- }
建立struts2的核心配置文件:struts.xml代码如下:<o:p></o:p>
- xml version="1.0" encoding="UTF-8" ?>
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <include file="struts-default.xml" />
- <package name="default" extends="struts-default">
- <action name="index" class="com.test.struts2.IndexAction" method="sayHello">
- <result name="success" type="freemarker">/main1.ftlresult>
- action>
- package>
- struts>