java web mvc的项目_springmvc web项目搭建

开发环境

myeclipse+maven+tomcat

步骤

1.创建maven项目

66458584_1

然后两次next

66458584_2

继续next

66458584_3

将项目的gourpId,artifactId,版本填写好,点击finish。maven项目创建完成

2.将maven项目转为web项目

在main下新建文件夹webapp

66458584_4

选中项目,右键

66458584_5

点击Browse,选择刚才创建的webapp文件夹,部署路径一栏自己命名,然后点击finish,项目就转为一个web项目

66458584_6

打开webapp文件夹,web项目的配置文件已经创建好了

66458584_7

3.配置pom.xml

因为是基于springmvc的web项目,并且页面需要用到jstl标签,所以需要增加如下依赖,

UTF-8

3.2.4.RELEASE

1.2

org.springframework

spring-webmvc

${spring.version}

jstl

jstl

${jstl.version}

修改packaging标签为war

war

4.配置web.xml

在main下创建resources文件夹,并且配置为Source

66458584_8

在resources下新建applicationContext.xml,具体内容如下

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

继续在resources下新建dispatcher.xml,内容如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

p:prefix="/views/"

p:suffix=".jsp"/>

上面两个文件新增完成后,配置web.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/classes/dispatcher.xml

1

dispatcher

*.html

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

characterEncodingFilter

/*

http://java.sun.com/jsp/jstl/core

/WEB-INF/tld/c.tld

http://java.sun.com/jsp/jstl/fmt

/WEB-INF/tld/fmt.tld

http://java.sun.com/jsp/jstl/functions

/WEB-INF/tld/fn.tld

http://www.springframework.org/tags

/WEB-INF/tld/spring.tld

在web.xml中,我们新增了jsp-config的配置,这是将这些用到的标签放到了项目本地,目的是避免网络下载

66458584_9

在dispatcher.xml中,我们配置了视图的存放路径,所以需要新建这个路径文件

66458584_10

5.业务代码

UserController.java

@Controller

@RequestMapping("/user")

public class UserController {

@RequestMapping("/register")

public String register(){

return "user/register";

}

@RequestMapping(method=RequestMethod.POST)

public ModelAndView createUser(User user){

ModelAndView mv = new ModelAndView();

mv.setViewName("user/createSuccess");

mv.addObject("user", user);

return mv;

}

}

User.java

public class User {

private String userName;

private String password;

private String realName;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getRealName() {

return realName;

}

public void setRealName(String realName) {

this.realName = realName;

}

}

register.jsp

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

新增用户" method="post">
用户名:
密码:
姓名:

createSuccess.jsp

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

创建用户成功

恭喜!${user.userName}创建成功

这样一个完整的springmvc的demo工程新建完成。

然后部署到tomcat下,进行测试一下

66458584_11

http://localhost:8080/smvc/user/register.html中的smvc自己在转web项目的时候的命名,如果不知道,可以再tomcat的部署路径下查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值