美文网首页
SpringMVC中一个控制器如果是void放回类型怎么指定跳转

SpringMVC中一个控制器如果是void放回类型怎么指定跳转

作者: 尘与曦 | 来源:发表于2019-08-29 18:14 被阅读0次

首先在spring-mvc.xml文件中配置视图解析器

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
 </bean>

配置一个action控制器

@Controller
@RequestMapping("/*")
public class MessageAction
{
    @RequestMapping("msg")
    public void get(Message msg, Model model)
    {
        model.addAttribute("msg", msg);
    }
}

又两个jsp页面分别是index.jsp和msg.jsp

<html>
<head>
    <base href="<%=basePath%>">
    <title>index</title>
</head>
<body>
<form action="/ssmone/msg.action" method="post">
    title:<input type="text" name="title" id="title" value="zxc" /><br>
    content:<input type="text" name="content" id="content" value="zixingc"/><br>
    <button type="submit">提交</button>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>${msg.title}</h1>
    <h1>${msg.content}</h1>

</body>
</html>

通过实验可以发现,当没有指定跳转view页面时,SpringMVC会自动根据当前的映射路径对应的路径和名称下的jsp。如上面的控制器代码,则会查找/ssmone/msg.jsp,而不是根据方法名查找!

相关文章

网友评论

      本文标题:SpringMVC中一个控制器如果是void放回类型怎么指定跳转

      本文链接:https://www.haomeiwen.com/subject/lclwectx.html