struts2和springMVC的区别 struts2配置文件详解


struts2和springMVC的区别 struts2配置文件详解

文章插图
—应用Struts2框架 , 开发一个加法器 , 采用两个页面 , 一个页面输入数据 , 另一个界面输出结果 。
一、新建maven项目
1.选择菜单file—new—maven project , 勾选“Create a &simple project (skip archetype selection)”项:
2.新建maven项目 , 设置项目属性如下:
1.配置web.xml
Maven项目最重要的配置文件是pom.xml , pom是“项目对象模型”的意思 。现在pom.xml中有一个错误 , 提示缺少web.xml:
展开目录src—main—webapp , 在里面新建文件夹“WEB-INF” , 在“WEB-INF”中创建web.xml 。
web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?><web-app><welcome-file-list><welcome-file>add.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>web.xml里面定义了主页名称 , 并将页面处理交给struts 。
2.实现控制器Action(代码如下)
package com.cqvie.action;import com.opensymphony.xwork2.ActionSupport;public class AddAction extends ActionSupport {private int n1;private int n2;private int sum;public int getN1() {return n1;}public void setN1(int n1) {this.n1 = n1;}public int getN2() {return n2;}public void setN2(int n2) {this.n2 = n2;}public int getSum() {return sum;}public void setSum(int sum) {this.sum = sum;}/*** 鐧诲綍* @return*/public String add() {sum=n1+n2;//System.out.println(sum);return "success";}}AddAction.java3.配置struts.xml(代码如下)
在编写好Action(控制器)的代码后 , 还需要进行配置才能让Struts2识别Action 。在src下创建文件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.i18n.encoding" value="https://www.520longzhigu.com/diannao/utf-8"></constant><constant name="struts.multipart.maxSize" value="https://www.520longzhigu.com/diannao/20971520" /><constant name="struts.devMode" value="https://www.520longzhigu.com/diannao/true" /><package name="p_user" namespace="/" extends="struts-default"><!-- 计算 --><action name="add" class="com.cqvie.action.AddAction"method="add"><result name="success">/add_success.jsp</result></action></package></struts>struts.xml4.编写jsp(代码如下)
本例共有两个页面add.jsp以及add_success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ 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=UTF-8"><title>计算页面</title></head><body><%-- <s:form action="add" method="post">n1:<s:textfield name="login.n1" />n2:<s:textfield name="login.n2" /><s:submit value="https://www.520longzhigu.com/diannao/等于"/></s:form> --%><form action="add" method="post">n1:<input type="text" name="n1" /><br>+<br>n2:<input type="text" name="n2" /><br><input type="submit" value="https://www.520longzhigu.com/diannao/等于" /></form></body></html>add.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ 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=UTF-8"><title>计算结果</title></head><body><%-- <s:form action="login" namespace="/" method="post"><s:textfield name="name" label="name"></s:textfield><s:password name="password" label="password"></s:password><s:submit value="https://www.520longzhigu.com/diannao/Login"></s:submit></s:form> --%><s:property value="https://www.520longzhigu.com/diannao/n1"/>+<s:property value="https://www.520longzhigu.com/diannao/n2"/>=<s:property value="https://www.520longzhigu.com/diannao/sum"/><br>结果等于:<s:property value="https://www.520longzhigu.com/diannao/sum"/></body></html>add_success.jsp5.运行项目


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: