java表单单击路径_spring:设置映射访问路径 或 xml配置访问路径 (spring mvc form表单)...

项目hello,

在src/main/java下面建一个目录: charpter2

一.xml配置访问路径

web.xml

Archetype Created Web Application

chapter2

org.springframework.web.servlet.DispatcherServlet

1

chapter2

/

chapter2-servlet.xml

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

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

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

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

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

http://www.springframework.org/schema/mvc

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

http://www.springframework.org/schema/context

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

chapter2/HelloWorldController.java

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

public class HelloWorldController implements Controller {

public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {

// TODO Auto-generated method stub

ModelAndView mv = new ModelAndView();

//添加模型数据 可以是任意的POJO对象

mv.addObject("message", "Hello World!");

//设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面

mv.setViewName("hello");

return mv;

}

}

注意在 chapter2-servlet.xml中添加处理器配置:

然后再WEB-INF/目录下新建jsp目录

web-inf/jsp/hello.jsp

hello

dddd

${message}

访问地址是: 项目地址/hello/helloworld

二。映射访问配置

首先配置文件有3: applicationContext.xml, web.xml, chapter2-servlet.xml

web.xml

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

chapter2

org.springframework.web.servlet.DispatcherServlet

1

chapter2

/

chapter2-servlet.xml

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

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

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

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

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

http://www.springframework.org/schema/mvc

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

http://www.springframework.org/schema/context

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

applicationContext.xml

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

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

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

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

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

http://www.springframework.org/schema/mvc

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

http://www.springframework.org/schema/context

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

r然后就是java代码:

chapter2/Student.java

public class Student {

private Integer id;

private Integer age;

private String name;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

chapter2/StudentController.java

import org.springframework.stereotype.Controller;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.ui.ModelMap;

@Controller

public class StudentController {

@RequestMapping(value="/student", method=RequestMethod.GET)

public ModelAndView student()

{

return new ModelAndView("student", "command", new Student());

}

@RequestMapping(value="/assStudent", method=RequestMethod.POST)

public String addStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model)

{

model.addAttribute("name", student.getName());

model.addAttribute("age", student.getAge());

model.addAttribute("id", student.getId());

return "student_result";

}

}

jsp文件

student.js

Spring MVC表单处理

Student Information

姓名:
年龄
编号

student_result.jsp

spring mvc表单处理

提交的学生信息

名称:${name}
年龄:${age}
编号:${id}

访问地址是: 项目地址/hello/student

访问地址:项目地址/hello/addStudent

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值